emi
emi

Reputation: 19

C Issue with understanding an array of pointers

I did some google search on this already. The problem is I just find explanation what a char pointer or char pointer pointer does and simply what an array of char pointer is. Actually my program is working. Simply when I close my eyes and accept everything the world is just fine but I constantly get some questions flashing in my mind, which I cant answer. Long story short I hope nobody will kill me for my question, since it could seem trivial to some of you,obviously it is not to me! So her we go:

char *pc;
char **ppc;
char *string[2] = {"Hello","World",};
ppc = string;
pc = *ppc;


printf("%p\n",ppc);
printf("%p\n",&pc);
printf("%p\n",*ppc);   
printf("%p\n",pc);
printf("%p\n",&string[0]);  //or &string
printf("%c\n", **ppc);
printf("%s", *ppc);

after running this I was expecting *ppc to be the address of first string (first character of first string), which is not and I was also expecting kind of relation between &pc and ppc which is also not the case. ppc is a pointer to pointer, but I literraly have no clue about the pointer which ppc is pointing before reaching the first element of string? I am sorry if this all sounds stupid, but I really want to understand it. Any help will be appreciated!

Upvotes: 1

Views: 104

Answers (5)

Gaurav Sehgal
Gaurav Sehgal

Reputation: 7542

Inside | | are the values and inside * * are the addresses.

| H | E | L | L | O |
*100*

| W | O | R | L | D |
*200*

| 100 | 200 |   //string
 *300*  

Now ppc = string, so

 ppc=>
       | 300 |
        *400*

Now pc = *ppc, so pc = *300 which is 100

 pc=>
      | 100 |
       *500*

Now if you do printf("%s", pc);, you will get the string stores at 100 which is HELLO.

Upvotes: 0

gsamaras
gsamaras

Reputation: 73366

Your code invokes Undefined Behavior, since %p excpets a pointr to void, thus all your print statements must cast their arguments to void*, like this for example printf("%p\n", (void*) ppc);.

Fix this and now draw your pointers, like this for example:

enter image description here

I was expecting *ppc to be the address of first string (first character of first string), which is not

It is!

Check printf("%p\n", (void*) ppc); and printf("%p\n", (void*) &string[0]);-they produce the same output.

I was also expecting kind of relation between &pc and ppc

  • ppc has the address of the first string ("Hello").
  • *ppc has the address of the first character of the string ('H').
  • pc has the address of the first character of the string ('H').
  • &pc has the address of pc.

So your expectation is false, pc and *ppc should have the same address, not &pc.


Further explanation:

string is an array of char pointers. char **ppc; is a double pointer to char.

Here:

ppc = string;

you set ppc to point to the address of the first element of string.

This:

pc = *ppc;

makes pc point to where ppc is actually pointing, that is the first element of string, i.e. string[0].

Upvotes: 2

Michel Billaud
Michel Billaud

Reputation: 1826

A picture etc. chars are yellow, char* orange, char** blue enter image description here

The letter 'e' can be obtained as pc[1], string[0][1] *ppc[1] etc.

Upvotes: 1

haccks
haccks

Reputation: 106022

After ppc = string;, ppc points to the first string of array string, i.e. it points to the string "Hello" (note that array string will decay to pointer to its first element in this case).

*ppc is a pointer that points to first character of the array "Hello".
pc = *ppc; makes pc to point to the same character of the same string "Hello"`.

That makes:
ppc will have address of the first string.
*ppc will have address of the first character of the first string.

Upvotes: 0

unwind
unwind

Reputation: 399863

The variable string is an array of pointers to characters, obviously it has to go somewhere, the actual array that is.

When you do

ppc = string;

you copy the address of the first element of string to ppc, and then

pc = *ppc;

copies the contents of that, i.e. the value of string[0].

Upvotes: 0

Related Questions