pointer to an array of pointers

R

ramu

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

Regards
 
V

vippstar

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;
 
M

Malcolm McLean

ramu said:
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 = malloc(32);
sprintf(strings, "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.
 
V

vippstar

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 = malloc(32);
sprintf(strings, "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 **?
 
C

CBFalconer

.... 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.
 
B

Barry Schwarz

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] 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 is the i-th pointer in the
array.

Which one did you mean?


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top