Thread: need help with structures

  1. #1
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246

    need help with structures

    Code:
    struct vix {
         char *pname;
         int day;
         int month;
         int year;
         int hour;
         int min;
     } ;
     
    struct vix *pvix;
    
    pvix = malloc ( siz * sizeof  *pvix );
    
    for ( j = 0; j < siz; j++ )
    fscanf(fp,"%d.%d.%d %d:%d %s",&(pvix+j)->day,&(pvix+j)->month,&(pvix+j)->year,
    &(pvix+j)->hour,&(pvix+j)->min,(pvix+j)->pname);
    
    for ( i = 0; i < siz; i++ )
       printf("%d %d %s\n",i,(pvix+i)->day,(pvix+i)->pname);
    
    free(pvix);
    Why does this give me an error at run time?...

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Why does this give me an error at run time?...

    Where is (pvix+j)->pname pointing? (You need to allocate memory for it.)
    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.*

  3. #3
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    >You need to allocate memory for it.
    how would i do that?

    btw the file contains something like

    01.01.2003 00:00 test
    11.11.2002 10:30 test2
    etc...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > how would i do that?
    Well the easy way is to make your structure member an array
    char pname[20];

    > &(pvix+j)->day
    Too complicated
    Try
    &pvix[j].day
    Just like what you would do if it were a regular array
    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.

  5. #5
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    ok, now there's no more errors but the output is strange :
    0 0 ?
    1 0 ?
    2 0 ?
    etc...
    i use this to print :
    Code:
    for ( i = 0; i < siz; i++ )
         printf("%d %d %s\n",i,pvix[j].day,pvix[j].pname);

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    for ( i = 0; i < siz; i++ )
         printf("%d %d %s\n",i,pvix[j].day,pvix[j].pname);
    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
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    haha i guess i'm blind , thanks Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM