Thread: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’

    I am encountering warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’ . can anybody enlightening me?

    Code:
    Date *date_create(char *datestr)
    {
    
    
    Date *Date_Structure = malloc(sizeof(Date));
    if(Date_Structure == NULL){
    return NULL;
    }   
    else
    {   
    
    
    char delims[] = "/";
    Date_Structure -> day = atoi(strtok(datestr,delims));
    Date_Structure -> month = atoi(strtok(NULL, delims));
    Date_Structure -> year = atoi(strtok(NULL, delims));
        
    scanf("%d/%d/%d",Date_Structure->day,Date_Structure->month,Date_Structure->year);
    
    return Date_Structure;
    }
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by man 3 scanf
    Synopsis

    #include <stdio.h>

    int scanf(const char *format, ...);
    <snip>
    Description

    The scanf() family of functions scans input according to format as described below. This format may contain conversion specifications; the results from such conversions, if any, are stored in the locations pointed to by the pointer arguments that follow format. Each pointer argument must be of a type that is appropriate for the value returned by the corresponding conversion specification.
    Note the parts I put in bold. scanf requires pointers to the location where it will store the scanned results. You need to use the address-of operator (&).

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Why do you set the day/month/year values with the atoi calls and then immediately try to scanf new values into those same locations overwriting what was there? Do you expect the scanf call to fail and the atoi calls to serve as backup initialization in the case that this does happen?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    The sscanf call will fail, because the strtok() calls will have trashed the string in the first place.

    On the other hand, it's just as strange to try and read the data from a string and from stdin.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-24-2011, 05:37 AM
  2. Replies: 3
    Last Post: 12-11-2009, 10:44 PM
  3. Replies: 2
    Last Post: 12-07-2009, 10:26 PM
  4. Replies: 3
    Last Post: 04-15-2008, 02:16 AM
  5. Replies: 17
    Last Post: 03-06-2008, 02:32 PM