question about a compiler warning

This is a discussion on question about a compiler warning within the C Programming forums, part of the General Programming Boards category; Here's a simple program that uses a pointer. The program compiles but issues this warning. The compiler I use is ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25

    question about a compiler warning

    Here's a simple program that uses a pointer. The program compiles but issues this warning. The compiler I use is gcc.

    gcc -g -ansi -pedantic -Wall ex9-2-4.c -o ex9-2-4
    ex9-2-4.c: In function 'main':
    ex9-2-4.c:12 warning: int format, pointer arg (arg 2)


    Here's the code.

    Code:
    /* ex9-2-4.c */
    
    #include <stdio.h>
    
    int main(void)
    {
    	int cost, *p_cost;
    	p_cost = &cost;
    
    	cost = 100;
    
    	printf("\nPointer value: %d, points at value: %d\n\n", p_cost, *p_cost);
    
    	return 0;
    }
    What conversion specifier do I use for pointers?

    P.S. If this question has been asked before on this message board, I'd appreciate someone giving me a link to the appropriate post. Thanks.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Code:
    printf("\nPointer value: %p, points at value: %d\n\n", (void*)p_cost, *p_cost);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25
    No more warnings given by the compiler. Thanks a lot :-))

    Quick follow up question about the output of the program, if I may.

    Output of printf("\nPointer value: %d, points at value: %d\n\n", p_cost, *p_cost);

    Pointer value: 2293620, points at value: 100

    Output of printf("\nPointer value: %p, points at value: %d\n\n", (void*)p_cost, *p_cost);

    Pointer value: 0022FF74, points at value: 100

    Why is the output different? Is it just a different way of indicating a pointer value?

    Thanks.
    Last edited by sashaKap; 03-17-2009 at 09:31 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The first one shows the pointer as a decimal number, the second shows it in hexadecimal notation. It's the same value (I think - it's about 2.2 million from the hex number - since you have recompiled, it's not guaranteed to be the same address anyways).

    It is actually not strictly defined what %p outputs, just that it's compatible with displaying pointers. To most people, the value itself is not very important, it's more a case of understanding which pointer is which or the difference between pointers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25
    Thanks a lot. It's clear as day now :-))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler warning and a confusing function declaration.
    By PaulBlay in forum C Programming
    Replies: 3
    Last Post: 05-20-2009, 10:18 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Quick Warning Question
    By Paul22000 in forum C Programming
    Replies: 1
    Last Post: 05-02-2008, 05:20 PM
  4. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  5. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 06:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21