Pointer to pointer

D

david

hi friends, don't understand this:

/*
**p is a pointer to *a[], but if *a stops increasing when reads abc,
where is pointing *p??
*/

#include <stdio.h>

int main(void)
{
char *a[] = {"abc", "def", "ghi", "jkl", NULL};
char **p;

p = (char **)a;
while (*p) {
while (**p) {
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
(*p)++;
}
p++;
}

return 0;
}

/*
Returns
david@debian:~$ ./demo
*a=0x4005ec *p=0x4005ec a abc
*a=0x4005ed *p=0x4005ed b bc
*a=0x4005ee *p=0x4005ee c c
*a=0x4005ef *p=0x4005f0 d def -----> Here *a stops inc, ¿where is
pointing *p?
*a=0x4005ef *p=0x4005f1 e ef
*a=0x4005ef *p=0x4005f2 f f
*a=0x4005ef *p=0x4005f4 g ghi
*a=0x4005ef *p=0x4005f5 h hi
*a=0x4005ef *p=0x4005f6 i i
*a=0x4005ef *p=0x4005f8 j jkl
*a=0x4005ef *p=0x4005f9 k kl
*a=0x4005ef *p=0x4005fa l l
*/
 
J

James Kuyper

david said:
hi friends, don't understand this:

/*
**p is a pointer to *a[], but if *a stops increasing when reads abc,
where is pointing *p??
*/

#include <stdio.h>

int main(void)
{
char *a[] = {"abc", "def", "ghi", "jkl", NULL};
char **p;

p = (char **)a;

In most contexts, an array name is automatically converted into a
pointer to the first element of the array, so the simple expression 'a'
already has the type "char**", there's no need for the cast. If a cast
were necessary, it would also indicate a design error.
while (*p) {
while (**p) {
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);

The "%p" format specifier expects a void* argument. This is one of the
few places where a cast is routinely needed and perfectly correct:

printf("*a=%p *p=%p %c %s\n", (void*)*a, (void*)*p, **p, *p);
(*p)++;
}
p++;
}

return 0;
}

/*
Returns
david@debian:~$ ./demo
*a=0x4005ec *p=0x4005ec a abc
*a=0x4005ed *p=0x4005ed b bc
*a=0x4005ee *p=0x4005ee c c

Up to this point, p == a, so *p == *a, or in other words, *p == a[0].
Then you execute p++. The result is that p == a+1, so *p == *(a+1), or
in other words *p == a[1].
*a=0x4005ef *p=0x4005f0 d def -----> Here *a stops inc, ¿where is
pointing *p?

Since p is now pointing at a[1], (*p)++ no longer changes the value of
*a. It is now changing the value of *(a+1).
 
B

Ben Bacarisse

david said:
hi friends, don't understand this:

/*
**p is a pointer to *a[], but if *a stops increasing when reads abc,
where is pointing *p??
*/

#include <stdio.h>

int main(void)
{
char *a[] = {"abc", "def", "ghi", "jkl", NULL};
char **p;

p = (char **)a;

I'd drop the cast.
while (*p) {
while (**p) {
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);

Technical matter: %p expects arguments of type void * and the
conversion is not done automatically so you need to write:

printf("*a=%p *p=%p %c %s\n", (void *)*a, (void *)*p, **p, *p);

Initially, *a and *p refer t the same object (a[0] -- the first
pointer in a). when you...

increment *p, you also change *a. But as soon as you...

move p on to the next element (a[1]), *a will remain the same.
 

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

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top