-2

I want to create a program that prints the address of an int, a float and a double.

int main()
{
    int a;
    float b;
    double c;
    printf("\na:%d \nb:%f \nc:%lf", &a, &b, &c);
}

But in the end all I get is the address of the int. For the other two the answer is 0.00000.

jchamp
  • 172
  • 3
  • 11
Maria
  • 110
  • 1
  • 12

3 Answers3

2

The correct format specifier for printing a memory address (pointer) is %p. You might as well cast the arguments into (void*) as the standard says that %p requires its arguments to be of type void*.

Using the wrong format specifier leads to Undefined Behavior.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
2

Use specifier %p to print address.

printf("\na:%p \nb:%p \nc:%p",(void *)&a,(void *)&b,(void *)&c);
ameyCU
  • 16,489
  • 2
  • 26
  • 41
1

use %p to print address of void. printf doesn't seem to be able to pointer to float directly.

Try this: printf("\na:%p \nb:%p \nc:%p", (void*)&a, (void*)&b, (void*)&c);

MikeCAT
  • 73,922
  • 11
  • 45
  • 70