Thread: list of structs

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    116

    list of structs

    hi guys i'm having the following problem with my code.
    I have a struct that represents a list :

    Code:
    struct list_of_elements{
       
        int number_of_elements;  //counter of list elements
        A* head;  //1st element of the list
        A* tail;    //last element of the list
    
    };
    So from the above my list contains elements of type A which is another struct.

    Code:
    struct A{
    
       int telephone_num;
       char* name;
    
    };

    When I initialize the list here's what I do:
    Code:
    struct list_of_elements *loe = (struct list_of_elements*) malloc ( 5*  sizeof(struct A*)); //I want the max size of the list to be 5 elements	
    	
    	for(int j=0;j<5;j++){
    		
    		list_of_elements[i] = NULL;
    	}
    	
    	list_of_elements->head=NULL; 
    	list_of_elements->tail=NULL;  
    	list_of_elements->number_of_elements=0;
    I have 2 questions:

    a)Is the initialization of Block correct as it contains an element of type char* which at the moment is undefined?

    b)Is the use of malloc correct?In general I'm a bit confused with the whole thing...

    Thanks in advance!

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by quo View Post
    hi guys i'm having the following problem with my code.
    I have a struct that represents a list :
    Wait, which one do you mean: an array or a list? You seem to be mixing the two. Your description and first structure indicates a list, but your code tries to use it like an array.

    This,
    Code:
    struct list_of_elements {
        int number_of_elements;
        struct element *head;
        struct element *tail;
    };
    is a fairly typical list handle, but I'd expect the elements to look like this:
    Code:
    struct element {
        struct element *next;
        int telephone_number;
        char *name;
    };
    Note the struct element *next. That's what makes above a singly-linked list element: it contains a pointer to the next element in list. (If you have a second one, say struct element *prev, pointing to the previous element in the list, then you have a doubly-linked list.)

    On the other hand, if you want an array, the two structures are typically something like
    Code:
    struct array_of_elements {
        int elements_allocated;
        int elements_used;
        struct element *element;
    };
    
    struct element {
        int telephone_number;
        char *name;
    };
    Which is it, an array or a list?

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by Nominal Animal View Post

    Which is it, an array or a list?
    Yes I must have been a bit confused..No I certainly want a list of elements.

    So for a list how should I initilize it?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by quo View Post
    b)Is the use of malloc correct?In general I'm a bit confused with the whole thing...
    No, it isn't.
    There is an idiom which makes allocating memory straight forward:
    Code:
    ptr = malloc(sizeof(*ptr));
    This line allocates the right amount of memory for an object "ptr" points to regardless of the type of the object.
    Here are some examples:
    Code:
    int *ptr;
    ptr = malloc(sizeof(*ptr));   // allocates memory for one int
    ...
    double *ptr;
    ptr = malloc(sizeof(*ptr));   // allocates memory for one double
    ...
    struct my_struct *ptr;
    ptr = malloc(sizeof(*ptr));   // allocates memory for one struct my_struct
    The malloc()-line is always the same. Pretty simple, isn't it?

    If you want to allocate memory for an array, just multiply sizeof() with the number of elements:
    Code:
    int *ptr;
    ptr = malloc(sizeof(*ptr) * 5);   // allocates memory for an array of 5 ints
    Bye, Andreas

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by quo View Post
    So for a list how should I initilize it?
    You usually start with a head node and then add the other nodes when you need them. You have to allocate memory for each node separately.

    I've the impression you don't really know how lists work, do you?

    There are two articles about linked lists on this site:
    FAQ > Linked List Issues - Cprogramming.com
    Linked Lists in C Tutorial - Cprogramming.com

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by AndiPersti View Post
    You usually start with a head node and then add the other nodes when you need them. You have to allocate memory for each node separately.

    I've the impression you don't really know how lists work, do you?

    There are two articles about linked lists on this site:
    FAQ > Linked List Issues - Cprogramming.com
    Linked Lists in C Tutorial - Cprogramming.com

    Bye, Andreas
    Ok thank you I think I've understood now.
    I have one more question
    When it comes to arrays and I want to allocate an array of structs then how should I do it ?
    Like this:

    Code:
    struct A **a = (struct A **) malloc ( 4 *  sizeof(struct A*));
    or like this:

    Code:
    struct A *a = (struct A*) malloc ( 4 *  sizeof(struct A*));
    Both allocate an array of pointers type struct A .Right?
    What's the difference?And for both next step is this?

    Code:
    for(int i=0;i<4;i++){
     A[i].element1=0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The first one allocates space for 4 pointers to struct A. Since you want a dynamic array of struct A objects, that is wrong, unless you're going to get the pointers to point to struct A objects.

    The second one also allocates space for 4 pointers to struct A, but then uses a pointer to struct A to store the return value of malloc. This is also wrong.

    For what you want, you should write:
    Code:
    struct A *a = malloc(4 * sizeof(*a));
    Now, space is allocated for 4 struct A objects, and a[0] is the first struct A object, assuming that malloc does not return a null pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by laserlight View Post
    For what you want, you should write:
    Code:
    struct A *a = malloc(4 * sizeof(*a));
    Now, space is allocated for 4 struct A objects, and a[0] is the first struct A object, assuming that malloc does not return a null pointer.
    Thank you for your answer.So if I do what you suggest then I do not need to do this as well for every object right?

    Code:
    a[i] = (A *) malloc (sizeof(struct A));
    This is what I did for option2 from the 2 options I've mentioned in my previous post

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should not do that since each element of the dynamic array is a struct A object. If you used to first version, then doing that would be correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs in list
    By justanme in forum C Programming
    Replies: 1
    Last Post: 12-09-2010, 06:01 PM
  2. Help crating a linked list of array of structs.
    By doubty in forum C Programming
    Replies: 1
    Last Post: 07-05-2009, 08:49 PM
  3. Sorting a list of structs
    By Jan79 in forum C++ Programming
    Replies: 9
    Last Post: 06-24-2003, 01:42 AM
  4. STL list::sort() sorting structs
    By hk_mp5kpdw in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2002, 07:23 AM
  5. CAN'T FIGURE IT -- structs on a linked list
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-01-2002, 05:22 AM