Thread: nested structs

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so unless you are using a strange compiler that accepts this, 2_ARRAY is STILL not a valid name in C.

    Fixing that will remove SOME of the problems with your code.

    The code is very badly indented - you really need to fix that up, which hides the fact that you are misising two end-braces in the code.

    You should not use "struct name" when name is a typedef'd name.

    loop, sort, a and b are unused in the main function.

    dbmARRAY1 is not defined.

    You are includig regex.h, but not using any regular expressions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    I have changed the names of the original structs....but here is the corrected one....
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<regex.h>
     
    #define ARRAY_SIZE 340
    #define STRUCT_SIZE 340
     
    #define MAXTOKENS       256
    #define MAXLINE         1024     /* fgets buff */
    #define MINLEN          3        /* skip lines shorter as */
    #define NUL '\0'
    #define TRUE 1
    #define FALSE 0
     
    char *strtok( char *str1, const char *str2 );
    typedef structn ARRAY1{
            char time[ARRAY_SIZE];
            char date[ARRAY_SIZE];
    }ARRAY1,ARR2;
     
    typedef struct first{
          char name[10];
           ARRAY1 *main;
       }first;
     
    typedef struct second{
          char db[10];
            2_ARRAY *other;
        }second;
     
    char delims[] = "=";
    char **split(char *string, char *delim);
    int trim(char *str);
     
         first first_info;
         second second_info;
     
        ARRAY1 ARRAY1_info;
          ARR2 ARR2_info;
    
    int main()
    {
    
     printf ("TEST \n" );
            FILE *data_txt,*dbm_txt;
            char *delim = "=";
            char **tokens = NULL;
    int i = 0, mcount = 0, lcount = 0;
            int loop;
            int sort;
            int a=0;
            int b=0;
            int j; 
    int count = 0, cnt = 0;
    char line[MAXLINE],line2[MAXLINE];
     
            struct ARRAY1 *str_ptr;   
            struct ARR2 *new_ptr;
          str_ptr = &ARRAY1_info;
          new_ptr = &ARR2_info;
     
           struct first *ptr;
           struct second *new;
            ptr = &first_info;
            new = &second_info;
     
            data_txt = fopen("db2cfg.txt","r");
            dbm_txt = fopen("db2cfg2.txt","r");
     int read = 0;
     while (fgets(line,100,data_txt)!=NULL)
     {
    trim(line);
     strcpy(first_info.name,"ccmlmd");
            if ((strlen(line) > 1)&&(read!=0)){
            lcount++;
            tokens = split(line, delim);
            for(i = 0; tokens[i] != NULL; i++) {
            if (i==0){
            if (tokens[i]!=NULL){
     //  printf("%02d: time %s ", i, tokens[i]);
            strcpy(first_info.main[count].time,tokens[i]);
           }
          }
      if (i==1){
          if(tokens[i]!=NULL){
    //      printf("%02d: date %s", i, tokens[i]);
     strcpy(first_info.main[count].date,tokens[i]);
            }
         }
       }
      for(i = 0; tokens[i] != NULL; i++)
      free(tokens[i]);
      free(tokens);
      count++;
      lcount++;
          }
     
          for(i=0; i < count; i++) {
     printf("%d %s %s", i,first_info.main[i].time, first_info.main[i].date);
    
          }
     
      i = 0;
    fclose(data_txt);
    // start db2cfg2
     read = 0;
    while (fgets(line2,100,dbm_txt)!=NULL)
    {
    trim(line2);
    
    strcpy(second_info.db,"ccmlmd");
    //    printf("other %s",second_info.db);
            if ((strlen(line2) > 1)&&(read!=0)){
            mcount++;
            tokens = split(line2, delim);
            for(i = 0; tokens[i] != NULL; i++) {
            if (i==0){
       if (tokens[i]!=NULL){
    //   printf("%02d: time %s ", i, tokens[i]);
     strcpy(second_info.other[cnt].time,tokens[i]);
           }
          }
      if (i==1){
          if(tokens[i]!=NULL){
    //      printf("%02d: date %s", i, tokens[i]);
     strcpy(second_info.other[cnt].date,tokens[i]);
            }
         }
       }
      for(i = 0; tokens[i] != NULL; i++)
      free(tokens[i]);
      free(tokens);
      cnt++;
     } // add for(j=0 ;j<=cnt
     j = 0;
          for(j=0; j < cnt; j++) {
     printf("%d %s %s", j,second_info.other[j].time,second_info.other[j].date);
          }
     
            fclose(dbm_txt);
            return 0;
            exit(0);
     }
    Can you confirm if this line is correct ?
    strcpy(first_info.main[count].time,tokens[i]);

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > 2_ARRAY *other;
    Why do you persist with this, when it's plainly wrong.

    Rename it to ARRAY2

    Or something even more descriptive.
    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.

  4. #19
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    Yes i must have missed it when I substituted the array names...
    could you please substitute 2_ARRAY as ARR2

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    No, you do it in YOUR code, then tell us what happened.
    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.

  6. #21
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    still getting the
    "test.c", line 136: syntax error before or at: *

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Funny, because line 136 in your code appears to be:
    Code:
     j = 0;
          for(j=0; j < cnt; j++) {
     printf("%d %s %s", j,second_info.other[j].time,second_info.other[j].date);
    But there isn't a "*" in the line at all.

    Are you posting errors belonging to another bit of code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    Can you confirm if this line is correct?

    strcpy(first_info.main[count].time,tokens[i]);

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It depends on what you mean by "correct". The line is syntactically and semantically correct, but I would still expect the program to blow up at this point, since I don't see anywhere where you actually make that main pointer point to anything in particular, let alone an actual array of things.

    (And I assume that you have fixed the typo of "structn" in your code?

  10. #25
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    Can you help me with pointing the pointer
    I am not sure where to initialize the pointer.

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The key word you are looking for is "malloc".

  12. #27
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    yep got it...its working....thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. Nested structs
    By Sir Andus in forum C Programming
    Replies: 5
    Last Post: 11-28-2006, 10:31 AM
  3. Nested Structs
    By Monkey83 in forum C Programming
    Replies: 2
    Last Post: 09-15-2006, 11:03 AM
  4. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 02:00 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM