Why does printing the first element of a pointer to pointer to a char print the contents of a string?

Maxime Van Hees

Example code

int main() {

char *s = "kut";
char **p = &s;
printf("*s:     %c\n", *s);
printf("s:      %p\n", s);
printf("&s[0]:  %p\n", &s[0]);
printf("p:      %p\n", p);
printf("*p:     %p\n", *p);
printf("**p:    %c\n", **p);
printf("p[0]:   %s\n", p[0]);
printf("&p[0]:  %p\n", &p[0]);

return 0;
}

Output:

*s:     k
s:      0x1043acf46
&s[0]:  0x1043acf46
p:      0x7ffeeb853670
*p:     0x1043acf46
**p:    k
p[0]:   kut
&p[0]:  0x7ffeeb853670

How does it come that &p[0] (address of p[0]) does print out the string kut? The output show that the address of p[0]is the same as p (as p is just a constant pointer pointing to the first element of the array).

Some programmer dude

For any pointer or array p and index i, the expression p[i] is exactly equal to *(p + i).

If i == 0 then we have p[0] which is equal to *(p + 0) which in turn is equal to *(p) which is the same as *p.

And in your case *p is the same as s.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does printing a pointer print the same thing as printing the dereferenced pointer?

From Dev

Why do printing a char pointer yield string instead of memory address?

From Dev

Why does this function return the correct length of a string? (Incrementing a char pointer)

From Dev

why does derefrencing a pointer to an array of integers(in 2d array) return(or decay to) pointer to first element?

From Dev

A pointer that does not print the first three words in a sentence

From Dev

Why does function taking char pointer pointer not work?

From Dev

Why does the char pointer not get updated to NULL

From Dev

How does a pointer to the constant pointer of the first element of an array work?

From Dev

Why sizes of an array and a pointer to a first element are different?

From Dev

why is this filename parameter a char pointer instead of string?

From Dev

Why isn't my char array pointer printing?

From Dev

Issue with printing char pointer in C

From Dev

Printing the value of pointer to char in C

From Dev

Passing pointer to first string element as buffer

From Dev

How to print pointer contents in Swift

From

Is there an idiomatic go way to print string pointer contents or nil?

From Dev

Why does dereferencing a char** value (pointer-to-pointer-to-char) differ from dereferencing a char*[] (pointer-to-char array)?

From Dev

Is const char* a string or a pointer

From Dev

print each char of string array with double pointer in C++

From Dev

Initialize and print const char pointer

From Dev

print char pointer value in c

From Dev

Incrementing pointer on string results in pointer to char not on string

From Dev

storing and printing string in void pointer

From Dev

why print out a char pointer yield the entire array instead of the memory addr of the pointer?

From Dev

Char Pointer to Pointer Array - get size of first pointer array

From Dev

Why does printing char sometimes print 4 bytes number in C

From Java

Why does printing a char[] array not print any output?

From Dev

Using pointer to print each element in a string by C++

From Dev

C printing values of array with pointer values to a char

Related Related

  1. 1

    Why does printing a pointer print the same thing as printing the dereferenced pointer?

  2. 2

    Why do printing a char pointer yield string instead of memory address?

  3. 3

    Why does this function return the correct length of a string? (Incrementing a char pointer)

  4. 4

    why does derefrencing a pointer to an array of integers(in 2d array) return(or decay to) pointer to first element?

  5. 5

    A pointer that does not print the first three words in a sentence

  6. 6

    Why does function taking char pointer pointer not work?

  7. 7

    Why does the char pointer not get updated to NULL

  8. 8

    How does a pointer to the constant pointer of the first element of an array work?

  9. 9

    Why sizes of an array and a pointer to a first element are different?

  10. 10

    why is this filename parameter a char pointer instead of string?

  11. 11

    Why isn't my char array pointer printing?

  12. 12

    Issue with printing char pointer in C

  13. 13

    Printing the value of pointer to char in C

  14. 14

    Passing pointer to first string element as buffer

  15. 15

    How to print pointer contents in Swift

  16. 16

    Is there an idiomatic go way to print string pointer contents or nil?

  17. 17

    Why does dereferencing a char** value (pointer-to-pointer-to-char) differ from dereferencing a char*[] (pointer-to-char array)?

  18. 18

    Is const char* a string or a pointer

  19. 19

    print each char of string array with double pointer in C++

  20. 20

    Initialize and print const char pointer

  21. 21

    print char pointer value in c

  22. 22

    Incrementing pointer on string results in pointer to char not on string

  23. 23

    storing and printing string in void pointer

  24. 24

    why print out a char pointer yield the entire array instead of the memory addr of the pointer?

  25. 25

    Char Pointer to Pointer Array - get size of first pointer array

  26. 26

    Why does printing char sometimes print 4 bytes number in C

  27. 27

    Why does printing a char[] array not print any output?

  28. 28

    Using pointer to print each element in a string by C++

  29. 29

    C printing values of array with pointer values to a char

HotTag

Archive