Thread: struct problem

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    15

    struct problem

    I have a hardware board with a 80186 proc. The time on the chip is set bij the function:

    Code:
    void rtxSetTimeDate(TimeDateStruct far* tds)
    the struct is defined as following:

    Code:
    typedef struct tag_time
    {
     unsigned char sec;  //seconds 0-59
     unsigned char min;  //minutes 0-59
     unsigned char hr;    //hours 0-23
     unsigned char dy;    //day 1-31
     unsigned char mn;   //month 1-12
     unsigned char yr;     //year 0-99
     unsigned char dow; //day of week (mon=1 - sun =7) 
     unsigned char dcen; //century if time/date is correct
    }
    I have declared a struct of this type: DateTime

    The date and time is sent by telnet to the hardwareboard in the format: yyyymmdd hhmmss. I have 2 variables for the date and time:

    Code:
    char *time,*date;
    now i try to put the data in the struct:


    Code:
    sscanf(date,"%4s%2s%2s",DateTime.yr,DateTime.mn,DateTime.dy);
    sscanf(time,"%2s%2s%2s",DateTime.hr,DateTime.min,DateTime.sec);
    
    rtxSetTimeDate(&DateTime);
    it just returns NULL for all values

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    try passing pointers to the objects in the sscanf call instead of just the object.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    ok now i have this:

    Code:
    sscanf(date,"%4s%2s%2s",*DateTime.yr,*DateTime.mn,*DateTime.dy);
    sscanf(time,"%2s%2s%2s",*DateTime.hr,*DateTime.min,*DateTime.sec);
    
    rtxSetTimeDate(&DateTime);
    I get the error: invalid indirection

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    By pointer to the object he was saying pass in the address of using the & which will actually modify the data
    Woop?

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    Ok i get it, i have changed it to:

    Code:
    sscanf(date,"%2s%2s%2s%2s",&DateTime.dcen,&DateTime.yr,&DateTime.mn,&DateTime.dy);
    sscanf(time,"%2s%2s%2s",&DateTime.hr,&DateTime.min,&DateTime.sec);
    
    rtxSetTimeDate(&DateTime);
    for testing, i've sent the following date and time to the hardware board: 20050915 121533

    I check the code with printf(the stdout of the hardware board is telnet):

    Code:
    printf("year:%s month:%s day:%s",DateTime.yr,DateTime.mn,DateTime.dy);
    printf("hours:%s minutes:%s seconds:%s",DateTime.hr,DateTime.min,DateTime.sec);

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Are you changing your struct to strings as well? Otherwise, you may want to use temporary ints to scan the data into, and then copy to your struct. (I'm assuming you instead want "little ints".)
    Code:
    #include <stdio.h>
    
    typedef struct tag_time
    {
       unsigned char sec;  // seconds 0-59
       unsigned char min;  // minutes 0-59
       unsigned char hr;   // hours 0-23
       unsigned char dy;   // day 1-31
       unsigned char mn;   // month 1-12
       unsigned char yr;   // year 0-99
       unsigned char dow;  // day of week (mon=1 - sun =7)
       unsigned char dcen; // century if time/date is correct
    } TimeDateStruct;
    
    int main(void)
    {
       static const char date[] = "20051013", time[] = "150937";
       int dcen, yr, mn, dy, hr, min, sec;
       TimeDateStruct DateTime = {0};
       if ( sscanf(date,"%2d%2d%2d%2d", &dcen, &yr, &mn,  &dy) == 4 )
       {
          DateTime.dcen = dcen;
          DateTime.yr   = yr;
          DateTime.mn   = mn;
          DateTime.dy   = dy;
       }
       if ( sscanf(time,"%2d%2d%2d", &hr, &min, &sec) == 3 )
       {
          DateTime.hr  = hr;
          DateTime.min = min;
          DateTime.sec = sec;
       }
       printf("DateTime.dcen = %d\n", DateTime.dcen);
       printf("DateTime.yr   = %d\n", DateTime.yr);
       printf("DateTime.mn   = %d\n", DateTime.mn);
       printf("DateTime.dy   = %d\n", DateTime.dy);
       printf("DateTime.hr   = %d\n", DateTime.hr);
       printf("DateTime.min  = %d\n", DateTime.min);
       printf("DateTime.sec  = %d\n", DateTime.sec);
       return 0;
    }
    
    /* my output
    DateTime.dcen = 20
    DateTime.yr   = 5
    DateTime.mn   = 10
    DateTime.dy   = 13
    DateTime.hr   = 15
    DateTime.min  = 9
    DateTime.sec  = 37
    */
    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.*

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    thank you very much! it works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 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. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  5. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM