Thread: Structure question

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    20

    Structure question

    Hi,

    I'm just thinking about the best idea for this scenario.

    I want a global structure to hold information, however when imputing this information, I want to use a parse function that will parse a text file and copy the contents into an array of structs...

    This is the question...how can I allocate memory, after its already been declared. Obviously if i have x number I want x number of array of structs.

    I will then use the struct in multiple functions after.

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by pc_doctor View Post
    This is the question...how can I allocate memory, after its already been declared. Obviously if i have x number I want x number of array of structs.
    You can't - but you don't have to. Instead, make your global member a pointer to an array, and allocate when the program initialises. Obviously, you'll need some other way to determine the number of elements in an array (an int n_structs parameter or a zeroed-out entry or something).

    e.g.:

    Code:
    int n_infos;
    struct info (*global_info)[];
    
    /* initialise */
    n_infos = 5;
    global_info = calloc(n_infos, sizeof((*global_info)[0]));
    /* error if global_info is NULL here! */
    Edit: Although this means you'd access individual elements with (*global_info)[x] - you could probably make your life easer by just declaring it a "struct info *global_info" and access it with global_info[x] instead...
    Last edited by JohnGraham; 04-14-2011 at 09:31 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you have
    Code:
    typedef struct { members; } fooType;
    You can maintain a dynamic array of them by starting with
    Code:
    fooType *myArray = malloc( howManyYouWant * sizeof(*myArray) );
    But for simplicity, you might want to use limited data sets and constant sized arrays for initial testing.
    Code:
    fooType myArray[SOMESIZE];
    Turning working array code into working malloc code is very easy.
    Debugging errant malloc code can make you crazy
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure question
    By kolliash in forum C Programming
    Replies: 1
    Last Post: 06-05-2008, 09:55 AM
  2. Question on structure
    By Doxygen in forum C Programming
    Replies: 2
    Last Post: 09-29-2006, 11:00 PM
  3. question about structure?
    By vice in forum C Programming
    Replies: 1
    Last Post: 04-17-2005, 04:46 AM
  4. structure question
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-17-2002, 09:17 PM
  5. structure question
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2002, 08:59 AM