gcc Compiler and Pointers

This is a discussion on gcc Compiler and Pointers within the C Programming forums, part of the General Programming Boards category; Hi there, The first sample program that I am reading on the book has the following code: Code: * Demonstrates ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    gcc Compiler and Pointers

    Hi there,

    The first sample program that I am reading on the book has the following code:
    Code:
    * Demonstrates basic pointer use. */
    
    #include <stdio.h>
    
    /* Declare and initialize an int variable */
    
    int var = 1;
    
    /* Declare a pointer to int */
    
    int *ptr;
    
    int main( void )
    {
        /* Initialize ptr to point to var */
    
        ptr = &var;
    
        /* Access var directly and indirectly */
    
        printf("\nDirect access, var = %d", var);
        printf("\nIndirect access, var = %d", *ptr);
    
        /* Display the address of var two ways */
    
        printf("\n\nThe address of var = %d", &var);
        printf("\nThe address of var = %d\n", ptr);
    
        return 0;
    }
    When I compile it using the gcc compiler, it gives the error:

    ptr.c: In function ‘main’:
    ptr.c:26:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
    ptr.c:27:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
    Is this a compiler error or is there a proper syntax for pointers using the gcc compiler?

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Yes, the correct way to print a pointer is using the %p format.
    Code:
        printf("\n\nThe address of var = %p", (void*)&var);
        printf("\nThe address of var = %p\n", (void*)ptr);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Quote Originally Posted by Salem View Post
    Yes, the correct way to print a pointer is using the %p format.
    Code:
        printf("\n\nThe address of var = %p", (void*)&var);
        printf("\nThe address of var = %p\n", (void*)ptr);
    So whenever the pointer address is to be printed, the syntax above should be used? Thanks.

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    283
    When you find yourself asking, "Is this a gcc compiler error, or is the code wrong?"..... it would be the latter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-13-2010, 09:02 PM
  2. pointers and arrays: my compiler complains
    By violatro in forum C Programming
    Replies: 12
    Last Post: 06-09-2010, 10:14 AM
  3. Replies: 4
    Last Post: 09-12-2009, 01:10 PM
  4. Replies: 2
    Last Post: 02-04-2008, 01:34 AM
  5. hp iPAQ 6300 && C compiler || C# compiler
    By xddxogm3 in forum Tech Board
    Replies: 2
    Last Post: 12-07-2004, 06:28 AM

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