Thread: structs of structs

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    structs of structs

    Hi All,

    Sorry - this is my second nested data structure post today

    Code:
    struct base
    {
            char *string;
    };
    
    struct parent
    {
            struct base my_base;
    };
    
    int main()
    {
            struct parent *my_parent;
            char temp_string[] = "temp string for test";
    
            my_parent->my_base.string = temp_string;
    
            printf("String = %s\n",my_parent->my_base.string);
    
            return 0;
    }
    I want to point the base char array at an existing string. Why does this cause problems?

    Thanks again,

    rotis23

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What problem are you getting? It compiled and ran fine for me.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Segmentation fault on Linux.
    Last edited by rotis23; 09-06-2005 at 09:04 AM.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Did you allocate space for the char* in base?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    No - because it's pointing to an existing char* at the time of the printf. Maybe this is where I'm going wrong?

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Hmm you never know. Thantos has a point though, some things - especially char* based I find - are a PITA under Linux. Not sure why.

    I'd just try calloc'ing some space first.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    OK - this works OK:

    Code:
    struct base
    {
            char *string;
    };
    
    struct parent
    {
            struct base my_base;
    };
    
    int main()
    {
            struct parent *my_parent;
            char *temp_string;
    
            temp_string = (char*)malloc(21 * sizeof(char));
            //strcpy(temp_string,"temp string for test");
    
            my_parent->my_base.string = temp_string;
            strcpy(temp_string,"temp string for test");
    
            printf("String = %s\n",my_parent->my_base.string);
    
            return 0;
    }
    I just want to know if the struct initialisation and the use of char pointers is OK like this.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Allocate space for the parent object.
    Code:
    struct parent *my_parent;
    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.*

  9. #9
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Well I'm no guru but I know you shouldn't cast - it's evil and a shooting offense.

    Do this instead:

    Code:
    #include <stdlib.h>  /* include this along with others */
    
    int main ()
    {
    	char* string = calloc (sizeof(char), 20 * sizeof *string);
    	/* use */
    	free (string);
    	return 0;
    }
    Don't forget stdlib.h or it won't compile.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Two issues;

    1) The primary cause of your crash is that my_parent in main() is not initialised to point at anything.

    2) You might also want to initialise the string member of base so it actually points at an array of char, and then use strcpy(my_parent->my_base.string, temp_string); instead of my_parent->my_base.string = temp_string; In this case, it's not strictly incorrect, but probably doesn't do everything you intend (in particular, the assignment does NOT create a copy of temp_string).

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Yes - of course the problem was allocating space for the parent pointer - thanks Dave.

  12. #12
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Damn I knew that.
    ...
    Thanks dave.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  13. #13
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    And the correct way to allocate mem for a pointer to a struct is?

    Code:
    parent = (struct parent*)malloc(sizeof(struct parent*));
    Or should I be using calloc ahluka? I don't want to start the thread of death but is there any instance where a cast with malloc is considered correct/essential?

  14. #14
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    thanks Grumpy - Dave beat you to it slightly. I don't want to copy the string just point to it.

  15. #15
    ---
    Join Date
    May 2004
    Posts
    1,379
    I can see that it needs space allocated but why does it compile fine for me as it is?
    Strange isn't it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM