How to create array structure with memebers of different size

This is a discussion on How to create array structure with memebers of different size within the C Programming forums, part of the General Programming Boards category; All, I need some help creating an array of structure whose memory is allocated at run time. Also the structure ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    15

    How to create array structure with memebers of different size

    All,
    I need some help creating an array of structure whose memory is allocated at run time. Also the structure has members whose size are different and again memory is allocated at run time. For example.

    Code:
    typedef struct{
    double *a
    double *b
    }test;
    
     where test is an array of structures determined at run time such that
     test[0] would have members as
      a[10]
      b[20]
     
     test[1] would have members as
      a[40]
      b[60]
    
     test[2] would have members as
      a[15]
      b[25]
    size of each arrays a and b within the structure are also determined at run time. Would appreciate if someone could help me out here.

    Thanks

    pk68

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    You mean like:
    Code:
    test *data = malloc(sizeof(test) * 5);
    data[0].a = malloc(sizeof(double) * 10);
    data[0].b = malloc(sizeof(double) * 20);
    ?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    It needs to be

    data[0].a = malloc(sizeof(double) * 10);
    data[0].b = malloc(sizeof(double) * 20);

    data[1].a = malloc(sizeof(double) * 20);
    data[1].b = malloc(sizeof(double) * 40);
    ....
    ....

    Not sure if this is possible

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure it's possible. What can't you figure out how to do?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    I would appreciate if someone can provide the syntax to allocate memory to the structure and the structure members dynamically. I am new to C programming and struggling a little to get this going. The function I am supposed to write where the structure is used will be called from a different function. Depending on the ID that gets passed in the memory needs to be allocated. For example, if ID is 1 that would be the first structure element. When 2 is passed in for ID, I need to increment the structure array and then allocate the memory for the members of this structure ID based on the size of the elements that is passed in.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    So instead of using literal values like 5, 10, or 20, use variables instead.
    Code:
    test *data = malloc(sizeof(test) * ID);
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the size of an array of structs
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2008, 05:29 AM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  4. Replies: 11
    Last Post: 03-25-2003, 04:13 PM
  5. Class member array size with constuctor initalizer
    By rafe in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2002, 10:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21