Struct with a ptr to a dynamically allocated array of other structures :(
I can't really make that description any simpler. I need help accessing a certain element of a dynamically allocated array of structures, and my only access to the array is a pointer contained in another structure. Here are the structures:
Code:
struct Country
{
int continent;
char * name;
char * cap;
char * lang;
};
struct Database
{
struct Country * list;
int ncountries;
char * filename;
};
And here is my function that needs to find the first "empty" element of the array:
Code:
//insert_database: insert ctry into the right element of db list
void insert_database( struct Database * db, struct Country * ctry )
{
int insertAt;
if( db != NULL && ctry != NULL )
{
for( int i = 0, insertAt = 0; i < db->ncountries; i++ )
{
if( db[i].list == NULL )
{
insertAt = i;
printf( "Insert here: %d\n", insertAt );
//copy_country( db, ctry, i );
break;
}
}
}
}
Help?! I've been browsing the internet for help on this, and I can't find anything. This is confusing the crap out of me.