Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing pointers in C

Tags:

c

pointers

I was trying to understand something with pointers, so I wrote this code:

#include <stdio.h>  int main(void) {     char s[] = "asd";     char **p = &s;      printf("The value of s is: %p\n", s);     printf("The direction of s is: %p\n", &s);      printf("The value of p is: %p\n", p);     printf("The direction of p is: %p\n", &p);      printf("The direction of s[0] is: %p\n", &s[0]);     printf("The direction of s[1] is: %p\n", &s[1]);     printf("The direction of s[2] is: %p\n", &s[2]);      return 0; } 

When compiling it with gcc I get these warnings:

$ gcc main.c -o main-bin -ansi -pedantic -Wall -lm main.c: In function ‘main’: main.c:6: warning: initialization from incompatible pointer type main.c:9: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘char (*)[4]’ main.c:11: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘char **’ main.c:12: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘char ***’ 

(The flags for gcc are because I must be C89)

Why incompatible types of pointer? Isn't the name of an array a pointer to it's first element? So if s is a pointer to 'a', &s must be a char **, no? And why do I get the other warnings? Do I have to cast the pointers with (void *) in order to print them?

And when running I get something like this:

$ ./main-bin The value of s is: 0xbfb7c860 The direction of s is: 0xbfb7c860 The value of p is: 0xbfb7c860 The direction of p is: 0xbfb7c85c The direction of s[0] is: 0xbfb7c860 The direction of s[1] is: 0xbfb7c861 The direction of s[2] is: 0xbfb7c862 

How can the value of s and it's direction (and of course the value of p) be the same?

like image 209
alcuadrado Avatar asked Oct 13 '08 14:10

alcuadrado


People also ask

Can you print pointers in C?

Printing pointers. You can print a pointer value using printf with the %p format specifier. To do so, you should convert the pointer to type void * first using a cast (see below for void * pointers), although on machines that don't have different representations for different pointer types, this may not be necessary.

Can you print a void pointer in C?

To print the value of a pointer to an object (as opposed to a function pointer) use the p conversion specifier. It is defined to print void -pointers only, so to print out the value of a non void -pointer it needs to be explicitly converted ("casted*") to void* .

How do you print the address stored in a pointer in C?

How do I print the address stored in the pointer in C++? int *ptr = &var; printf("%p", ptr); That will print the address stored in ptr will be printed.

What are pointers in C?

A pointer is a variable that stores the memory address of another variable as its value. A pointer variable points to a data type (like int ) of the same type, and is created with the * operator.


2 Answers

"s" is not a "char*", it's a "char[4]". And so, "&s" is not a "char**", but actually "a pointer to an array of 4 characater". Your compiler may treat "&s" as if you had written "&s[0]", which is roughly the same thing, but is a "char*".

When you write "char** p = &s;" you are trying to say "I want p to be set to the address of the thing which currently points to "asd". But currently there is nothing which points to "asd". There is just an array which holds "asd";

char s[] = "asd"; char *p = &s[0];  // alternately you could use the shorthand char*p = s; char **pp = &p; 
like image 131
James Curran Avatar answered Sep 20 '22 18:09

James Curran


Yes, your compiler is expecting void *. Just cast them to void *.

/* for instance... */ printf("The value of s is: %p\n", (void *) s); printf("The direction of s is: %p\n", (void *) &s); 
like image 41
indiv Avatar answered Sep 21 '22 18:09

indiv