0
votes

I have tried to make a program with the parameter %d in a pointer but it does not work, I changed to %p, but that is for the address, I want to print the value. This is the error

gcc null_Pointer.c -o null_Pointer
null_Pointer.c: In function ‘main’:
null_Pointer.c:7:35: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]
    7 |    printf("The value of ptr is : %x\n", ptr  );
      |                                  ~^     ~~~
      |                                   |     |
      |                                   |     int *
      |                                   unsigned int
      |                                  %ls

this is the code

#include <stdio.h>

int main () {

   int  *ptr = NULL;

   printf("The value of ptr is : %x\n", ptr  );

   return 0;
}
4
Have you tried %p.Fiddling Bits
and cast the argument to void *, assuming you want to print the address held by the pointer.Sourav Ghosh
What output do you see? And what output did you expect?phonetagger
You would derefrence ptr, to print the value, like this: *ptr. If it's not pointing to a valid int address though you'd invoke undefined behavior.Fiddling Bits
"I changed to %p but that is for the address, I want to print the value" in your example you definitely try to print the pointer itself what is exactly what is meant by the address. The value would be *ptr, what would invoke UB in your case ptr == NULL.Ingo Leonhardt

4 Answers

4
votes

To print the value that the pointer points to, you have to dereference it with *.

printf("The value of ptr is: %d\n", *ptr);

But this will cause undefined behavior if ptr is NULL, you have to assign it to point to a valid int.

int a = 123;
int *ptr = &a;

Then it will print 123.

2
votes

To print the address value stored in ptr:

printf( "The value of ptr is %p\n", (void *) ptr );

The print the value of the thing ptr points to:

printf( "The value of what ptr points to: " );
if ( !ptr )
  printf( "(null)\n" );
else
  printf( "%d\n", *ptr );

If ptr is NULL, this code will print the string (null). If ptr is not NULL but otherwise invalid (doesn't point to an int object within that object's lifetime), then the behavior is undefined. You may get garbage output, you may get a runtime error, "The Lonely Goatherd" may start playing out of your computer, etc. There's no good way to test if a non-NULL pointer is valid, which is why it's a good idea to set unused pointer objects to NULL.

1
votes

The format string defines both: what kind (type) of value is expected and how it will be printed, then the corresponding argument needs to correspond.

If you chose %p the expected value must be of pointer type and will be printed in hexadecimal format. From the man:

p The void * pointer argument is printed in hexadecimal

If you want to print the value a pointer points to, then:

  1. your pointer must point to something
  2. you must dereference the pointer with operator *
  3. use any suitable format character

In your case, %d can be used to print an int in decimal format. From the man:

dor i The int (or appropriate variant) argument is converted to signed decimal (d and i)

To met the three criteria you can write something like:

int a = 666; // define an int variable
int *ptr;    // define a pointer to an int (actually not initialized)
ptr = &a;    // make the pointer points to the int named a, or, make the pointer takes the value of the address of variable a
print("%d\n",*ptr); // print as decimal the value pointed by ptr

If you want to print the value pointed by ptr in hexadecimal, then use %x.

0
votes

The proper format specifier for printing a pointer value is %p. You also need to cast the pointer to void * because printf doesn't know the exact type of the parameters it receives.

printf("The value of ptr is : %p\n", (void *)ptr);

Regarding your comment:

I changed to %p, but that is for the address

That's correct, and the address in question is the value of ptr.