57

When i run this code.

#include <stdio.h>

void moo(int a, int *b);

int main()
{
    int x;
    int *y;

    x = 1;
    y = &x;

    printf("Address of x = %d, value of x = %d\n", &x, x);
    printf("Address of y = &d, value of y = %d, value of *y = %d\n", &y, y, *y);
    moo(9, y);
}

void moo(int a, int *b)
{
    printf("Address of a = %d, value of a = %d\n", &a, a);
    printf("Address of b = %d, value of b = %d, value of *b = %d\n", &b, b, *b);
}

I keep getting this error in my compiler.

/Volumes/MY USB/C Programming/Practice/addresses.c:16: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c: In function ‘moo’:
/Volumes/MY USB/C Programming/Practice/addresses.c:23: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’

Could you help me?

Thanks

blargman

Matt
  • 22,721
  • 17
  • 71
  • 112
nambvarun
  • 1,201
  • 4
  • 13
  • 14
  • 2
    Those say "warning", not "error". That means that your program will still run. But do fix the warnings per the answer below. – Jim Balter Mar 13 '11 at 00:13

5 Answers5

100

You want to use %p to print a pointer. From the spec:

p The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.

And don't forget the cast, e.g.

printf("%p\n",(void*)&a);
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • that's what i originally thought but the tutorial on this website (http://www.cis.temple.edu/~ingargio/cis71/code/addresses.c) is telling me to use %d... is it wrong. – nambvarun Mar 12 '11 at 23:58
  • 3
    @blargman, yes, it's wrong. You might be able to coerce things into working by typecasting, but since `%d` is for printing signed integers, it's probably not a good choice. – Carl Norum Mar 13 '11 at 00:00
  • 8
    +1: ... but for portability (and to follow the **SHALL** from the Standard) don't forget to cast the address. `printf("%p\n",(void*)&a);` – pmg Mar 13 '11 at 00:07
  • 3
    @pmg, is the cast necessary? I thought conversions to and from `void *` are safe and automatic in C (6.3.2.2 paragraph 1). – Carl Norum Mar 13 '11 at 00:12
  • @pmg what do u mean by portability? – nambvarun Mar 13 '11 at 00:12
  • By portability is meant the possibility to compile/run the program with different compilers/operating systems/computers. http://en.wikipedia.org/wiki/Portability_%28software%29 – hlovdal Mar 13 '11 at 00:15
  • Thanks @hlovdal; that is indeed what I mean. – pmg Mar 13 '11 at 00:19
  • 6
    @Carl: in variadic functions the compiler cannot check the types of arguments against the expected types. The cast to `void*` is not automatic: what is automatic in variadic functions is a few low-range integers to `int`, and `float` to `double`. – pmg Mar 13 '11 at 00:24
  • @pmg - that makes sense. I found more about it in 6.5.2.2 after asking here. Have you ever worked on a machine where `void *` was incompatible with any other pointer types? – Carl Norum Mar 13 '11 at 00:28
  • @Carl: And similar to variadic functions, if you happen to call a function which the compiler have not seen a prototype for, it will assume int for all parameters, and sizeo(int) might be different from sizeof(void *), so it is important to be aware of these two cases. This specifically also applies to `NULL` which might be defined as just `0` which is of type int. E.g. `ret = execlp("ls", "ls", "-l", (void *)NULL);`. – hlovdal Mar 13 '11 at 00:36
  • @Carl: no, I have only worked with well-behaved machines. In ancient days, with TurboC (or whatever) I had a few programs where function pointers and data pointers had different size: `sizeof (void(*)()) != sizeof (void*)` – pmg Mar 13 '11 at 00:39
  • Actually the man page for execlp also says this: "The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL." – hlovdal Mar 13 '11 at 00:41
  • @pmg where do u learn these fascinating concepts? Could u please reference me to some books. Thanks for the fascinating discussion. – nambvarun Mar 13 '11 at 00:44
9

When you intend to print the memory address of any variable or a pointer, using %d won't do the job and will cause some compilation errors, because you're trying to print out a number instead of an address, and even if it does work, you'd have an intent error, because a memory address is not a number. the value 0xbfc0d878 is surely not a number, but an address.

What you should use is %p. e.g.,

#include<stdio.h>

int main(void) {

    int a;
    a = 5;
    printf("The memory address of a is: %p\n", (void*) &a);
    return 0;
}

Good luck!

Ron
  • 1,695
  • 1
  • 19
  • 15
  • 3
    The value `0xbfc0d878 ` *is* a number. `(void*)0xbfc0d878` is not. And `%p` is likely to use a human-readable representation that looks like a number (typically hex) but that doesn't mean pointers are numbers. (BTW, the question was answered more than 2 years ago.) – Keith Thompson Jun 13 '13 at 06:38
  • Why do we have to cast to `(void*)`? – Cristian Gutu Feb 01 '19 at 16:23
1

To print the address of a variable, you need to use the %p format. %d is for signed integers. For example:

#include<stdio.h>

void main(void)
{
  int a;

  printf("Address is %p:",&a);
}
Vinz
  • 5,997
  • 1
  • 31
  • 52
Amarendra Deo
  • 55
  • 1
  • 9
  • 2
    While this code may answer the question, providing additional context regarding **how** and **why** it solves the problem would improve the answer's long-term value. – Alexander Mar 20 '18 at 03:15
0

Looks like you use %p: Print Pointers

skaz
  • 21,962
  • 20
  • 69
  • 98
0

I tried in online compiler https://www.onlinegdb.com/online_c++_compiler

int main()
{
    cout<<"Hello World";
    int x = 10;
    int *p = &x;
    printf("\nAddress of x is %p\n", &x); // 0x7ffc7df0ea54
    printf("Address of p is %p\n", p);    // 0x7ffc7df0ea54

    return 0;
}
Praveer Kumar
  • 912
  • 1
  • 12
  • 25