C printf

I am having an issue understanding why the %x is not working in this code. Since int_ptr is a pointer it has a hex address in the memory so writing %x should work. Could anyone please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <stdio.h>

int main() {
   int int_var = 5;
   int *int_ptr;

   int_ptr = &int_var; // put the address of int_var into int_ptr

   printf("int_ptr = 0x%08x\n", int_ptr);
   printf("&int_ptr = 0x%08x\n", &int_ptr);
   printf("*int_ptr = 0x%08x\n\n", *int_ptr);

   printf("int_var is located at 0x%08x and contains %d\n", &int_var, int_var);
   printf("int_ptr is located at 0x%08x, contains 0x%08x, and points to %d\n\n", 
      &int_ptr, int_ptr, *int_ptr);
}
What problem are you having?
Works fine for me.


int_ptr = 0x0026f9ac
&int_ptr = 0x0026f9a0
*int_ptr = 0x00000005

int_var is located at 0x0026f9ac and contains 5
int_ptr is located at 0x0026f9a0, contains 0x0026f9ac, and points to 5


I am compiling it from the command line and this is what I am getting:


addressof2.c: In function ‘main’:
addressof2.c:9:4: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("int_ptr = 0x%08x\n", int_ptr);
^
addressof2.c:10:4: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int **’ [-Wformat=]
printf("&int_ptr = 0x%08x\n", &int_ptr);
^
addressof2.c:13:4: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("int_var is located at 0x%08x and contains %d\n", &int_var, int_var);
^
addressof2.c:15:7: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int **’ [-Wformat=]
&int_ptr, int_ptr, *int_ptr);
^
addressof2.c:15:7: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int *’ [-Wformat=]
Define "not working".

First of all %x writes unsigned int. There is no guarantee that pointer size = unsigne int size.
As there is no format specifier which takes uintptr_t, you need to manualy check your pointer size in program and then select correct format specifier.
closed account (1CfG1hU5)
line 10 of your code does work. %08x and &int_ptr is the memory address.

line 14, 15 a similar restatement of of 9 through 11 to one line, %d on 14 is also different.
%x is working for other two.

my compiler gave no compile errors.

if the value to screen of *int_ptr were negative, you will have problems with %x.

int = -32,768 to 32,767

unsgned int = 0 to 65535
Last edited on
jt1 wrote:

int = -32,768 to 32,767
unsgned int = 0 to 65535

That's only true for 16 bit compilers which are obsolete.

At OP:
Use %p if your compiler supports it. %p is a format spec specifically for pointers.

Last edited on
closed account (1CfG1hU5)
still use an older dos compiler from time to time where those values are true.

what are the new limits for int and unsigned int? and which compiler? visual studio?

turbo c++ 3.0 dos is used for freedos objects.

i still have the 1.01 dos compiler.

wrote some lottery number makers recently in VGAHI mode.

i have visual studio, but don't have that computer with me.

Thank jt1 for the help, and AbstractionAnon you're right I am gonna stick to %p which seems to be working fine.
closed account (1CfG1hU5)
just checked dos c++ lang. %p is in there too for pointers.
Last edited on
Topic archived. No new replies allowed.