Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print variable addresses in C?

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

like image 866
nambvarun Avatar asked Mar 12 '11 23:03

nambvarun


People also ask

How do you show the address of a variable?

A pointer basically stores the memory address of a variable as its value. To print pointer values, we use the %p format specifier.

How do you print the address of a variable in C in hexadecimal?

%x format specifier in printf used to print the address in hexadecimal format.

Where are variable address stored in C?

Variables on the stack So a pointer used by a function may be stored on the stack, and the address of that pointer can be calculated by doing pointer arithmetic on the stack pointer.

What is the data type of address in C?

The data type of a memory address is a pointer, which is denoted by the type that it points to, followed by an asterisk ( * ).


2 Answers

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); 
like image 79
Carl Norum Avatar answered Sep 29 '22 09:09

Carl Norum


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!

like image 34
Ron Nuni Avatar answered Sep 29 '22 08:09

Ron Nuni