Thread: Allocating memory to a structure

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    22

    Allocating memory to a structure

    I want to allocate memory to a structure which is a member of another structure. I can find many examples online how to allocate memory to an array of structures or a structure pointer, but what I have now is just one structure corr as a member of structure bbb.

    bbb is an array of structures within the structure aaa

    Code:
    struct bbb
    {
    int x;
    int y;
    struct corr ccc;
    }
    I have allocated memory to all bbb structures and I now want to allocate memory to:
    Code:
     aaa->bbb[co_b].ccc
    I have tried all of the following and every time I get a compiling error:

    Code:
    if ((&aaa->bbb[co_b].ccc = (struct corr*) malloc(sizeof (struct corr))) == NULL) {
            return EXIT_FAILURE;
        } 
    
    
    if ((aaa->bbb[co_b].ccc = (struct corr*) malloc(sizeof (struct corr))) == NULL) {
            return EXIT_FAILURE;
        }
    
    if ((aaa->bbb[co_b].ccc = (struct corr*) malloc(sizeof (struct corr*))) == NULL) {
            return EXIT_FAILURE;
        }
    The first one gives the error lvalue required as left operand of assignment.

    The last two give the error: incompatible types when assigning to type ‘struct corr’ from type ‘corr *’

    What would be the correct way to allocate memory to a structure in this case? Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    ccc isn't a pointer.

    You get a ccc whenever you allocate a bbb.

    If you had
    struct corr *ccc;

    Then
    aaa->bbb[co_b].ccc = malloc(sizeof (struct corr));
    should be good.


    See the FAQ on why casting malloc in a C program is a bad idea.
    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
    Dec 2017
    Posts
    1,633
    Since you only seem to want only the one corr struct in the bbb struct you may as well leave bbb's definition the way you have it in the first post and dispense with the malloc call since in that case it's not needed. It also means you don't need to free it, so it should simplify things (if you really only want only the one corr struct (and always want it) in the bbb struct).
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2014, 07:17 AM
  2. allocating memory for structure
    By baxy77bax in forum C Programming
    Replies: 2
    Last Post: 04-30-2011, 01:49 PM
  3. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  4. Allocating Memory for a Structure
    By surfxtc79 in forum C Programming
    Replies: 4
    Last Post: 06-05-2003, 11:40 AM
  5. Allocating memory for a structure member
    By dalek in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2003, 06:56 AM

Tags for this Thread