I have been given a .o file which creates a box_t ** and I have to use it.
Now, I don't know if it is
case 1:
an pointer to an array of box_t or
case 2:
an pointer to an array of box_t *
I wrote a simple code myself in creating box_t** in both the ways and accessed differently. It seems to be working fine in both the cases. Now, given a box_t **, and size_t n, number of elements in it, is it possible to know if it is case 1 or case 2 without any further information.
struct box_tag{
int pencils;
int pens;
};
typedef struct box_tag box_t;
box_t boxarray[10] = {{1,2},{3,4},
{5,6},{7,8},
{9,10},{11,12},
{13,14},{15,16},
{17,18},{19,20}};
box_t ** box_bundle;
Creation Version1:
box_t** create_dp(void)
{
box_bundle = (box_t **)malloc(sizeof(box_t **));
*box_bundle = boxarray;
}
Accessing Version 1:
int main ()
{
box_t * tmp = *box_bundle;
for (int i =0; i<10; i++)
{
printf("%d\n",tmp[i].pencils);
}
return 0;
}
Creation Version 2:
box_t** create_dp (void)
{
box_bundle = (box_t **)malloc(sizeof(box_t **));
*box_bundle = (box_t *)malloc (sizeof(box_t *) * 10);
for(int i=0; i<10;i++)
{
*(box_bundle +i ) = &boxarray[i];
}
}
Accessing Version 2:
int main ()
{
create_dp();
for(int i=0; i<10; i++)
{
box_t * tmp =*box_bundle++;
printf("pencils %d \n", tmp->pencils);
}
return 0;
}