Thread: Struct with a ptr to a dynamically allocated array of other structures :(

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So are you actually initializing the items you allocate to have NULL pointers, or are you just praying they randomly have that value?


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

  2. #2
    null
    Join Date
    Dec 2005
    Posts
    18
    Well, that's where I'm stuck. I dynamically allocate (with calloc) the space needed for my array of country structures:
    Code:
    db->list = (struct Country *)calloc( countries, sizeof( struct Country ) );
    After that, I don't really know how to work with that array anymore. I've never read or used anything that does this. Sorry

  3. #3
    null
    Join Date
    Dec 2005
    Posts
    18
    Some of the code provided by my teacher makes me doubt what I'm doing (not that I was ever sure):
    Code:
    	for( i = 0; i < db->ncountries; i++ )
    	{
    		delete_country( db->list+i );   // free mem alloc to a country
    	}
    I should be able to use that same "db->list+i" part to check the array, right? I'm just trying to find the first free element to insert my country into...
    Data structures are so much easier, but I'm stuck with these parameters!

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    After that, I don't really know how to work with that array anymore. I've never read or used anything that does this
    But anyway , using calloc , the space is initializied to zero.

    to loop the array :
    Code:
    int  i;
    
    for( i = 0; i < db->ncountries; i++ )
        db->list[i] = NULL;
        /* or
        *(db->list+i) = NULL;
        */

    Why don't u post the all code ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Replies: 6
    Last Post: 01-16-2007, 09:21 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM