Thread: comparison between pointer and integer

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

    comparison between pointer and integer

    Hi guys im getting
    In function `getMonth':
    cal.c:40: warning: int format, unsigned int arg (arg 3)
    cal.c:45: warning: comparison between pointer and integer
    cal.c:45: warning: comparison between pointer and integer

    Is that possible because of im passing the variables wrong through the function or because im reading it from fscanf wrong?


    Code:
    int main(void)
    {
       unsigned int month, year;
       
       getMonth(&month);
       
       return EXIT_SUCCESS;
    }
    
    
    /****************************************************************************
    * Function getMonth() prompt the user for a number MIN_MONTH to MAX_MONTH and
    * returns that number. The number 0 is valid because this indicates that the
    * user wants to select all months.
    ****************************************************************************/
    unsigned getMonth(unsigned int  *month)
    {
       
       /*** declare variables*/
      int arr[31];
      int month_days [] ={31,28,31,30,31,30,31,31,30,31,30,31};
      
      printf("Please enter a month between 0 - 12\n");
      fscanf(stdin, "%d", month); 
      
      /* validation check if month entered is less then 0 > 12*/
      while(1)
      {
         if(month<1 || month>12)
         {
           printf("error wrong month input between 0-12\n");
           break;
      
         } 
      } 
       
       return EXIT_SUCCESS;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    unsigned getMonth(unsigned int  *month)
    unsigned what? I think you want an unsigned int here.

    also, month is a pointer to an integer. You can't compare a pointer to an integer like you do here
    Code:
    if(month<1 || month>12)
    You should do *month<1, etc.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    fscanf(stdin, "%d", month);
    Code:
    cal.c:40: warning: int format, unsigned int arg (arg 3)
    Use %u for an unsigned int.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    unsigned getMonth(unsigned int  *month)
    unsigned what? I think you want an unsigned int here.
    The int keyword is optional, and the keywords can go in any order.
    Code:
    unsigned == unsigned int
    unsigned == int unsigned
    static long unsigned int == long unsigned static
    Code:
      /* validation check if month entered is less then 0 > 12*/
      while(1)
      {
         if(month<1 || month>12)
         {
           printf("error wrong month input between 0-12\n");
           break;
      
         } 
      }
    That code becomes an infinite loop if the user enters a valid month . . . .
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM