Thread: declaring instances of parent structs at the end of the struct, instead of in main

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    41

    declaring instances of parent structs at the end of the struct, instead of in main

    A quick question about declaring parent structs (structs with structs inside them):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    /*
     * 
     */
    
    
    struct basic_struct {
        
        int day;
        int month;
        int year;
        char date [12];
        
    };
    
    
    struct gsm_struct {
        
        char Callcomm[15];
        char Textcomm[15];
        char Hangupcomm[15];
        
    };
    
    
    struct dynamic_struct {
        
        struct basic_struct inner_struct;
        struct gsm_struct gsm;
        
    };
    
    
    int main(int argc, char** argv) {
    
    
        
        
        return (EXIT_SUCCESS);
    }

    In the above code, should I initialize instances of dynamic_struct at the end of the struct declaration? like so:

    Code:
    struct dynamic_struct {
        
        struct basic_struct inner_struct;
        struct gsm_struct gsm;
        
    } gsm, banana, example;

    More to my question, if I initialized and instance in the main loop, would it create structs within structs infinitely? I have read it is also best practice to declare a pointer to the infant struct within the parent struct, in order to avoid the same problem. like so:

    Code:
    struct dynamic_struct {
        
        struct basic_struct *inner_struct;
        struct gsm_struct *gsm;
        
    };
    That way you can initialize inside the main loop without infinite overflows. Is this true?

    Thanks in advance
    MedicineMan25
    Last edited by MedicineMan25; 09-19-2016 at 08:31 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,423
    > In the above code, should I initialize instances of dynamic_struct at the end of the struct declaration? like so:
    No, that just creates global variables with default initialisations.

    You should do this
    Code:
    int main ( ) {
      struct dynamic_struct gsm = {
        { /* init list for basic_struct */ },
        { /* init list for gsm_struct*/ }
      };
    
    }
    > if I initialized and instance in the main loop, would it create structs within structs infinitely?
    Not in this example it wouldn't.

    In fact the compiler won't even let you create infinite structures to begin with, so it isn't a problem you need to worry about accidentally running into.

    The only time you HAVE to worry about this is when you're creating recursive data structures like linked lists.
    Code:
    struct list {
      struct list *next;
    };
    A struct can contain pointers to itself, it just can't contain instances of itself.

    > struct gsm_struct *gsm;
    Pointers to other structs come in handy in a couple of circumstances.
    1. if gsm_struct is particularly large (say 10's of KB), then creating lots of instances of it directly on the stack might be a problem. In that case, you might prefer to allocate the bulk of the data via malloc instead.

    2. If you have a dynamic number of gsm connections to maintain, then you could do things like this
    var.gsm = malloc( numConnections * sizeof(struct gsm_struct) );
    This gives you say var.gsm[0].Callcomm to var.gsm[numConnections-1].Callcomm to play with.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    41


    The only time you HAVE to worry about this is when you're creating recursive data structures like linked lists.
    [/code]
    [code]
    Code:
    [COLOR=white !important]?

    1
    2
    3
    struct list {
    struct list *next;
    };



    Yes, I remember now that's where I read about this, whilst reading about linked lists. Something I have had to re-visit after actually learning structs properly.

    Code:
    > struct gsm_struct *gsm;
    
    Pointers to other structs come in handy in a couple of circumstances.
    1. if gsm_struct is particularly large (say 10's of KB), then creating lots of instances of it directly on the stack might be a problem. In that case, you might prefer to allocate the bulk of the data via malloc instead.


    I'll definitely have to remember that, really useful.


    Once again you are more than helpful, thanks Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring variables in int main()?
    By Programmer_P in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 02:21 AM
  2. Declaring instances of different implementations
    By dwks in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2008, 11:43 AM
  3. Replies: 15
    Last Post: 12-07-2006, 09:27 PM
  4. Struct containing instances of itself?
    By gibbofresco in forum C Programming
    Replies: 2
    Last Post: 09-25-2006, 12:25 PM
  5. register structs - problem declaring
    By Kagey in forum C Programming
    Replies: 11
    Last Post: 11-11-2002, 03:58 AM

Tags for this Thread