Re: Passing an array of structures from a pointer [3]
And you can try this code (which does also the heap allocation):
<code>
#include <stdlib.h>
#include <malloc.h>
typedef struct ddlbColl1{
long LINK_ID;
int LIST_NUMBER;
} COLL1;
typedef struct ddListBox{
int x;
long y;
struct ddlbColl1 *xxx;
} DDLB;
DDLB *obj_DDLB; // Eventually Won't be global!
DDLB* ULC_DDLB_config_ddlb (int x, int y);
void ULC_DDLB_ddlb ( DDLB *obj_DDLB);
void Free_DDLB( DDLB * p );
int main(void)
{
obj_DDLB = ULC_DDLB_config_ddlb (10,5);
ULC_DDLB_ddlb (obj_DDLB);
/* Release memory allocated on the heap */
Free_DDLB( obj_DDLB );
return 0;
}
COLL1 * Create_Coll1(void)
{
COLL1 * pColl1 = NULL;
int count = 2;
/* Create collection (array) on the heap */
pColl1 = (COLL1 *) malloc( sizeof(COLL1) * count );
/* Should check for errors - malloc() returning NULL ... */
/* TODO */
/* Initialize the COLL1 array */
pColl1[0].LINK_ID = 200;
pColl1[0].LIST_NUMBER = 4;
pColl1[1].LINK_ID = 201;
pColl1[1].LIST_NUMBER = 8;
/* Return pointer to that array to the caller */
return pColl1;
}
/* Releases heap memory */
void Free_DDLB( DDLB * p )
{
/* Release memory for pointed array */
if ( p->xxx != NULL )
{
free( p->xxx );
p->xxx = NULL;
}
/* Release memory for structure DDLB */
free( p );
}
DDLB* ULC_DDLB_config_ddlb (int x, int y)
{
obj_DDLB = (DDLB*) malloc (sizeof (struct ddListBox));
obj_DDLB->x = x;
obj_DDLB->y = y;
obj_DDLB->xxx = Create_Coll1(); //Assign the address of the DDLB_COLL1[]
array!
return obj_DDLB;
}
void ULC_DDLB_ddlb ( DDLB *obj_DDLB)
{
// implementation file which uses the infos from DDLB_COLL1 array!
long x;
x = obj_DDLB->xxx[0].LINK_ID; // x = -858993460 ?????
}
</code>
HTH,
Giovanni