1
 #include <stdio.h>

    int main(void)
    {
        int i = 3;
        int* j = &i;
        printf("%u",j);
    }

The above code should print out the address (an unsigned integer) of memory block in which integer 3 is contained. But instead I am getting this error- error: format specifies type 'unsigned int' but the argument has type 'int *'.

I confirmed from various sources that:
1. *j refers to "value at address stored in j"
2. &j refers to address of memory block in which pointer j is stored.
3. j contains an unsigned int value which is the address of memory block at which j is pointing.

limbo
  • 684
  • 9
  • 18
Vinz.R
  • 77
  • 1
  • 7

2 Answers2

4

Your %u is incorrect. j is a pointer, so you should use %p. This change should do:

printf("%p", (void *)j);
artm
  • 17,291
  • 6
  • 38
  • 54
2

As you a trying to print out the address you need the p flags - see printf manual page.

Hence the code should be

#include <stdio.h>
int main()
{
    int i = 3;
    int* j = &i;
    printf("%p",(void *)j);
}

See here

Ed Heal
  • 59,252
  • 17
  • 87
  • 127