0

I have to compile code in C89 for class, I wrote this code to see if it would compile. I compile it using,

gcc -std=c89 -pedantic

#include<stdio.h>

int main(void)
{
    int number;

    printf("%p", &number);
}

this code would compile just fine on a system with gcc version 4.1.2, but on a machine with version 5.3(Ubuntu 5.3), it would give me the error

warning:format '%p' expects argument of type 'void *', but argument 2 has type 'int *' [-Wformat=]

If anyone can give me a work around I would really appreciate it. Thank you

5
  • Are you actually using format specifier %d or %p? Are you trying to print the value or the address of number? Jul 2, 2016 at 4:07
  • I am trying to get the address
    – Sil
    Jul 2, 2016 at 4:24
  • my mistake i meant to put %p
    – Sil
    Jul 2, 2016 at 4:32
  • 2
    Really this isn't a question about Ubuntu. It isn't a question about any Linux operating system. You'd be getting this with the same GCC on a BSD, or on Windows. This is a pure programming language question.
    – JdeBP
    Jul 2, 2016 at 7:53
  • @Sil , gcc 4.1 that old compiler how did you get and which release you are using?
    – user.dz
    Jul 2, 2016 at 10:36

1 Answer 1

2

%p expects a void *, so casting to void * will do the trick.

#include <stdio.h>

int main(void)
{
    int number;

    printf("%p", (void *)&number);
}

Not the answer you're looking for? Browse other questions tagged .