Thread: nested structs

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    27

    nested structs

    Hi,
    I have 3 structs that I am trying to work with.
    Code:
    typedef struct{
            char time[ARRAY_SIZE];
            char date[ARRAY_SIZE];
    }ARRAY1, 2_ARRAY;
     
    typedef struct{
          char name[10];
           ARRAY1 *main;
       }first;
     
    typedef struct{
          char names[10];
           2_ARRAY *other;
        }second;
    How do I assign values to the second struct ?
    I am getting confused.

    lotta luv

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    2_ARRAY is not a valid name.

    Perhaps something like this?
    Code:
    first x;
    
    x.main = ... (something like malloc, perhaps)
    strcpy(x.main->time, "12:01:59");
    --
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you mean by "assign values to the second struct"? If you had a variable of type first, then you would use .name and .main to access the char array and ARRAY1 array, respectively.

    Side question: Do you intend for ARRAY1 and 2_ARRAY to mean the same thing?

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    oh ok...whats the syntax for assigning time through the "struct second"

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bandal27 View Post
    oh ok...whats the syntax for assigning time through the "struct second"
    Aside from the code not compiling as the name (2_ARRAY) starts with a digit, it would be similar to the first case.
    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.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    I also would not use the variable name of "*main".

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by slingerland3g View Post
    I also would not use the variable name of "*main".
    I do agree - don't call other things than the main entry function "main".
    But that is distinct from "main" because it's inside the struct itself - it can not be used on it's own - it will always be preceded by a variable name and . or ->



    --
    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. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > How do I assign values to the second struct ?
    Just add more dots.

    foo.outerMember.innerMember = 1;

    foo.outerPtr->innerMember = 2;

    etc etc
    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.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    thank you,
    Just one last question is the malloc needed here...?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you've got pointers, then you need to make them point somewhere.

    One popular way is to use malloc.
    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.

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    I am trying to get this to work.
    Code:
    typedef struct{
            char time[ARRAY_SIZE];
            char date[ARRAY_SIZE];
    }ARRAY1, 2_ARRAY;
     
    typedef struct{
          char name[10];
           ARRAY1 *main;
       }first;
     
    typedef struct{
          char names[10];
           2_ARRAY *other;
        }second;
     
           struct first *ptr;
           struct second *new;
            ptr = &first_info;
            new = &second_info;
    
    strcpy(first_info.main[count].key,tokens[i]);
    I am getting this error at

    strcpy(first_info.main[count].key,tokens[i]);
    line 115: syntax error before or at: *

    Please help

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    CORRECTION..
    this is the code having error

    strcpy(first_info.main[count].time,tokens[i]);
    line 115: syntax error before or at: *

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > strcpy(first_info.main[count].key,tokens[i]);
    So is this line of code actually inside a function?

    Also, having a member called main is going to be confusing, since there's also a function with the same name.

    A complete example which we can copy/paste into a compiler is much better than simply snipping the lines which you think are relevant.
    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.

  14. #14
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    Actually the line is in the main().
    Its not in any function.

    The code is too long.

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    Here is the code I am trying to run
    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 struct{
            char time[ARRAY_SIZE];
            char date[ARRAY_SIZE];
    }ARRAY1,2_ARRAY;
     
    typedef struct{
          char name[10];
           ARRAY1 *main;
       }first;
     
    typedef struct{
          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;
          dbmARRAY1 ARRAY12_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 dbmARRAY1 *new_ptr;
          str_ptr = &ARRAY1_info;
          new_ptr = &ARRAY12_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("&#37;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);
     }

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