"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