format ‘%X’ expects argument of type ‘unsigned int’, but argument 3 has type 'int *'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • giacomomarciani
    New Member
    • Jan 2012
    • 4

    format ‘%X’ expects argument of type ‘unsigned int’, but argument 3 has type 'int *'

    I don't understand why the piece of code below returns me the error:

    warning: format ‘%X’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int *’ [-Wformat]

    Code:
        int *v=malloc(10*sizeof(int));
        int i;
        for(i=0;i<10;i++) v[i]=i;
        for(i=0;i<10;i++) printf("v[i]=%+2d &v[i]=%#.10X | 
        *  (v+i)=%+2d (v+i)=%#.10X\n",v[i],&v[i],*(v+i),(v+i));
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    According to the C99 specification, %X takes an unsigned int argument, but you passed &v[i] which is an int*. Your compiler is warning you quite clearly of this mismatch. This mismatch may or may not be significant, it depends on the details of your compiler implementation.

    The portable way to print pointer values is with %p. This conversion specifier doesn't accept the # or .10 options.

    This forum provides links to two different language references. Curiously, one of these references tells us that %X takes an unsigned int, the other says it takes a signed int. I don't understand this discrepancy. Perhaps this is a difference between C and C++? However, this discrepancy doesn't matter to you -- the warning is warranted in either case.

    Comment

    • giacomomarciani
      New Member
      • Jan 2012
      • 4

      #3
      thank you! solved ;-)

      Comment

      Working...