Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer to an array of pointers

Reply
Thread Tools

pointer to an array of pointers

 
 
ramu
Guest
Posts: n/a
 
      12-03-2007
Hi,
Could anyone please tell me how to dereference a pointer to an
array of pointers?

Regards
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      12-03-2007
On Dec 3, 7:13 pm, ramu <ramu....@gmail.com> wrote:
> Hi,
> Could anyone please tell me how to dereference a pointer to an
> array of pointers?
>
> Regards


*p or p[0].

A pointer to an array of N pointers is char *(*)[N].

Example:

char *(*foo)[N] = NULL;
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      12-03-2007

"ramu" <> wrote in message
news:f0977e82-d97d-4124-8f94-...
> Hi,
> Could anyone please tell me how to dereference a pointer to an
> array of pointers?
>

/* set up a pointer to a list of pointers, here strings for readability */
char **strings;
int i;

strings = malloc(12 * sizeof(char *));
for(i=0;i<12;i++)
{
strings[i] = malloc(32);
sprintf(strings[i], "string %d", i+1);
}

/* dereference to get a string, or char * */
printf("%s\n", strings[3]);
/* dereference to get a character, should be the letter g */
printf("%c\n", strings[3][5]);

As you can see, when you say "dereference the pointer" you can mean either
get what it points to immediately, which is another pointer, or what it
points to ultimately, which in this case is a char.

Also we can use this syntax

/* treat as pointer to single element */
printf("%s\n", *strings);

/* get first element of first element */
printf("%c\n", **strings);

This isn't so useful as the first. Usually when you want a pointer it is
because you have an array of things to point to, pointers to single items
are less common. However you'll need to know both syntaxes.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      12-03-2007
On Dec 3, 11:20 pm, "Malcolm McLean" <regniz...@btinternet.com> wrote:
> "ramu" <ramu....@gmail.com> wrote in message
>
> news:f0977e82-d97d-4124-8f94-...> Hi,
> > Could anyone please tell me how to dereference a pointer to an
> > array of pointers?

>
> /* set up a pointer to a list of pointers, here strings for readability */
> char **strings;
> int i;
>
> strings = malloc(12 * sizeof(char *));
> for(i=0;i<12;i++)
> {
> strings[i] = malloc(32);
> sprintf(strings[i], "string %d", i+1);
>
> }
>
> /* dereference to get a string, or char * */
> printf("%s\n", strings[3]);
> /* dereference to get a character, should be the letter g */
> printf("%c\n", strings[3][5]);
>
> As you can see, when you say "dereference the pointer" you can mean either
> get what it points to immediately, which is another pointer, or what it
> points to ultimately, which in this case is a char.

When you dereference a pointer you mean access what it points to.
In strings[3][5] you're dereferencing two pointers. strings and
strings[3].

> Also we can use this syntax
>
> /* treat as pointer to single element */
> printf("%s\n", *strings);

*strings is equal to strings[0] and it has nothing to do with 'single
element'.
A pointer is of scalar type.

Really, i don't undestand your post, OP asked for a pointer to array
of pointers and you answered with a char **?
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-04-2007
wrote:
>

.... snip ...
>
> Really, i don't undestand your post, OP asked for a pointer to
> array of pointers and you answered with a char **?


A char** is a pointer to a pointer to char, which is what a
function will receive as a parameter to describe an array of
pointers to char. Remember that under most conditions an array is
described by a pointer to its zeroth element.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-06-2007
On Mon, 3 Dec 2007 09:13:14 -0800 (PST), ramu <>
wrote:

>Hi,
> Could anyone please tell me how to dereference a pointer to an
>array of pointers?
>


In strict terminology, a pointer to an array of pointers is
TYPE *arr[N];
TYPE *(*ptr)[N];
ptr = &arr;
ptr is a pointer to an array of N pointer to TYPE and points to one
such array.

In this case, ptr[0] is the array itself and ptr[0][i] is the i-th
pointer in the array.

Frequent, as in 99+%, newcomers to the language use the term to mean
TYPE *arr[N]
TYPE **ptr;
ptr =arr; /* or the equivalent ptr = &arr[0]; */
ptr is a pointer to pointer to TYPE and points to the first pointer in
the array of such pointers.

In this case, the slightly simpler ptr[i] is the i-th pointer in the
array.

Which one did you mean?


Remove del for email
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
pointer to array v/s array of pointers mann! C Programming 6 02-26-2005 12:59 AM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57