warning int format pointer arg

This is a discussion on warning int format pointer arg within the C Programming forums, part of the General Programming Boards category; Hi guys im trying to compile this code in ansi c and i keep getting a warning when using the ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    warning int format pointer arg

    Hi guys im trying to compile this code in ansi c and i keep getting a warning when using the printf statement when trying to print the phone number from the struct it gives me a

    warning: int format pointer arg what am i doing wrong in the printf statement



    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #define  PHONEMAX     9
    #define  AGEMAX       20
    #define  ADDRESSMAX   30
    void flush_stdin();
    
     struct personDetails
    {
       int phone    [PHONEMAX];
       int age      [AGEMAX];
       char gender;
       char address [ADDRESSMAX];
    
    
    };
    
    
    int main (void)
    {
       int valid;
       struct personDetails pd;
       
     
       do
       {
       
       printf("please enter an integer phone number\n\n!");  
      
     
       if((fscanf(stdin, "%d", pd.phone)==1))
       {
         printf("you have entered an int\n\n");
         valid = 1;
       }  
       else
       {
         flush_stdin();
         valid  = 0;
       }
      
       }
       while(!valid);
    
       printf("Your phone number is:%d", pd.phone);
       return EXIT_SUCCESS;
    
    }
    
    
    /* begin of flush_stdin 05-8-31 19:30 */
    void flush_stdin( void )
    {
    	if ( !feof(stdin) ) {
                    int c;
    		while( ( c=getchar() ) != '\n' && c != EOF )
    			;
    	}
    } /* end of flush_stdin */

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    What you are declaring with int phone[PHONEMAX] is an array of 9 separate integer numbers.

    The fscanf coincidentally "works" because it's expecting the address of the int, and when you specified pd.phone, the "phone" array decayed to a pointer, so you got the address of it.

    When you did the printf further down, you should have done pd.phone[0] because you wrote the int into the first element of the phone array (9 numbers).

    I expect this is not what you meant.

    It is better to store phone numbers as strings. People will inevitably try to put in symbols, leading zeroes, aribtrary amounts of area code. All this stuff makes it inappropriate for an integer.

    If you really wanted to store a phone number as an int, change it to simply int phone; and change the fscanf line to &(pd.phone).

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    As said above if you want to store it as telephone no. as int then declare phone no. as long or double.it would be better if you store as string(array of char).you can easily convert it to integer by subtracting its ascii of zero from that

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cbastard
    As said above if you want to store it as telephone no. as int then declare phone no. as long or double.
    I don't know about you, but my phone number doesn't have a decimal point in it.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by quzah
    I don't know about you, but my phone number doesn't have a decimal point in it.
    Quzah.
    I've seen this on TV that last 3-4 years, since the net became so popular.
    Code:
    555.555.5555

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, you can't store 555.555.5555 in a float, either. But maybe you can without an area code, like 555.5555.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Not to mention that some phone numbers have significant leading zeros which all number formats are going to trim away for you.
    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.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    And I think many European phone numbers are alphanumeric.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 02:09 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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