1

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!

4
  • *ppc is the address of the first string - see the output of your last printf Commented Sep 15, 2017 at 12:44
  • Related: stackoverflow.com/questions/4832082/…. Lots of near duplicates available, if you'd search a little. Commented Sep 15, 2017 at 12:47
  • also, the %p expects a pointer-to-void, so you must cast each of these arguments to (void*). Also, &string[0] and &string point to the same address, but different things. One says: "that's Mr. String", and the other "That's Mr. String's head". Commented Sep 15, 2017 at 13:05
  • All %p lines invoke undefined behaviour. Read the man-page carefully and use the correct type. Commented Sep 15, 2017 at 13:15

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

I like your image very much, the colors seem better than mine, nice answer! I have posted one as well, check it if you want! =)
There's an important detail : pc is the name of a "box" containing an address, string is the name of the address where there are 2 consecutive boxes. C so-called "arrays" are historically a hack around pointers (constant ones, except when used as parameters) not really "variables".
@gsamaras I used "dia" under Linux
0

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

Comments

0

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.

2 Comments

well that's the the question, are those the same?
No. They are not. ppc and *ppc are different though they have same address value.
0

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.

6 Comments

Shouldn't ppc=> | 100 | be ppc=> | 300 |?
when you do int a = b, you want a to have value of b, so ppc points to what string points to.
Perhaps I'm confused by your ppc=> notation. Is it supposed mean "this is the value of ppc", or "this is what ppc is pointing to"?
I realize it is misleading.ppc=> does not mean anything.It just says, do you want to see how ppc looks in memory?Go ahead and see below.
To me, it seems to indicate that ppc is stored at address 400 and contains the address 100. But it should contain the address 300 as that is the address of the first element of string in your diagram.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.