Dynamic array of structures containing yet another dynamic array of structures

This is a discussion on Dynamic array of structures containing yet another dynamic array of structures within the C Programming forums, part of the General Programming Boards category; Hello, I want to create an array of structures wherein the array is dynamic. Inside the parent structure, there is ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    Dynamic array of structures containing yet another dynamic array of structures

    Hello,
    I want to create an array of structures wherein the array is dynamic. Inside the parent structure, there is another array of structures which is also dynamic. I am not able to resolve some issues while trying to compile it...Kindly help me out.
    Firstly, i have the code below showing the structure details
    Code:
    typedef struct
    {
            uint8_t dlmAcid;
            uint8_t dlmHarqEBCount;
            uint8_t dlmAisn;
    } STRUCTPAD DlmAcidInfo_t;
    
    typedef struct
    {
            uint8_t cidInMap;
            uint8_t mapharqFlag;
            uint8_t noOfEBs;
            uint8_t mcsNo;
            uint8_t mapharqMode;
            uint8_t ackchAlloc;
            uint8_t mapAcidCount;
            DlmAcidInfo_t DlmAcid;
    } STRUCTPAD CidInfoInMap_t;
    
    typedef struct
    {
            uint8_t noOfCids;
            CidInfoInMap_t cidInfoInMap;
    } STRUCTPAD ZoneInfoInMap_t;
    
    typedef struct
    {
            uint8_t dlFrameNo;
            ZoneInfoInMap_t zoneInfoInMap;
    } STRUCTPAD HARQAckInfo_t;
    Here is my code, i have written. It incomplete as i am yet to free the memory.
    Code:
    #include<stdio.h>
    #include<stdint.h>
    #define STRUCTPAD  __attribute__((__packed__))
    typedef struct
    {
    	uint8_t dlmAcid;
    	uint8_t dlmHarqEBCount;
    	uint8_t dlmAisn;
    } STRUCTPAD DlmAcidInfo_t;
    
    typedef struct
    {
    	uint8_t cidInMap;
    	uint8_t mapharqFlag;
    	uint8_t noOfEBs;
    	uint8_t mcsNo;
    	uint8_t mapharqMode;
    	uint8_t ackchAlloc;
    	uint8_t mapAcidCount;
    	DlmAcidInfo_t DlmAcid;
    } STRUCTPAD CidInfoInMap_t;
    
    typedef struct
    {
    	uint8_t noOfCids;
    	CidInfoInMap_t cidInfoInMap;
    } STRUCTPAD ZoneInfoInMap_t;
    
    typedef struct
    {
    	uint8_t dlFrameNo;
    	ZoneInfoInMap_t zoneInfoInMap;
    } STRUCTPAD HARQAckInfo_t;
    
    int main()
    {
    int q,z,x,a;
    int noOfZones =2;
    #if 0
    HARQAckInfo_t temp;
    HARQAckInfo_t *harqInfo;
    harqInfo = &temp;
    #endif
    
    HARQAckInfo_t **harqInfo = NULL;
    ZoneInfoInMap_t **zoneInfoInMap = NULL;
    CidInfoInMap_t **cidInfoInMap = NULL;
    DlmAcidInfo_t **DlmAcid = NULL;
    
    for(q=0; q<1; q++)
    {
    harqInfo = (HARQAckInfo_t**)realloc(harqInfo, (q+1)*sizeof(HARQAckInfo_t*));
    harqInfo[q]= (HARQAckInfo_t*)malloc(sizeof(HARQAckInfo_t));
    /*Begin filling HARQAckInfo_t based on no. of zones which you can know dynamically */
    harqInfo[q]->dlFrameNo = 100;
    for(z=0; z<noOfZones; z++)
    	{
    	harqInfo[q]->zoneInfoInMap = (ZoneInfoInMap_t**)realloc(harqInfo[q]->zoneInfoInMap,(z+1)*sizeof(ZoneInfoInMap_t*));
    	harqInfo[q]->zoneInfoInMap[z] = (ZoneInfoInMap_t*)malloc(sizeof(ZoneInfoInMap_t));
    	harqInfo[q]->zoneInfoInMap[z]->noOfCids = 50;
    	for(x =0; x<harqInfo[q]->zoneInfoInMap[z]->noOfCids; x++)
    		{
    		harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap = (CidInfoInMap_t**)realloc(harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap,(x+1)*sizeof(CidInfoInMap_t*));
            	harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x] = (CidInfoInMap_t*)malloc(sizeof(CidInfoInMap_t));
    		harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->cidInMap = 5;
    		harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->mapharqFlag = 1;
    		harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->noOfEBs = 5;
    		harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->mcsNo = 4;
    		if(harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->mapharqFlag ==1)
    		{
    		harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->mapharqMode = 0;
    		harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->ackchAlloc = 0;
    		harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->mapAcidCount = 20;
    		for(a=0; a<harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->mapAcidCount; a++)
    			{
    			harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->DlmAcid = (DlmAcidInfo_t**)realloc(harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->DlmAcid,(a+1)*sizeof(DlmAcidInfo_t*));
    			harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->DlmAcid[a] = (DlmAcidInfo_t*)malloc(sizeof(DlmAcidInfo_t));
    			harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->DlmAcid[a]->dlmAcid = 5;
    			harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->DlmAcid[a]->dlmHarqEBCount = 5;
    			harqInfo[q]->zoneInfoInMap[z]->cidInfoInMap[x]->DlmAcid[a]->dlmAisn = 1;
    			}
    		}
    		}
    	}
    }
    }
    I am getting various warnings and errors as i am sure that i have done mistakes in declaring structures and assigning memory to it.Kindly see these errors after compilation
    Code:
    gcc dyn.c 
    dyn.c: In function `main':
    dyn.c:52: warning: cast to pointer from integer of different size
    dyn.c:58: warning: cast to pointer from integer of different size
    dyn.c:58: error: incompatible types in assignment
    dyn.c:59: error: subscripted value is neither array nor pointer
    dyn.c:60: error: subscripted value is neither array nor pointer
    dyn.c:61: error: subscripted value is neither array nor pointer
    dyn.c:63: error: subscripted value is neither array nor pointer
    dyn.c:63: error: subscripted value is neither array nor pointer
    dyn.c:63: warning: cast to pointer from integer of different size
    dyn.c:64: error: subscripted value is neither array nor pointer
    dyn.c:65: error: subscripted value is neither array nor pointer
    dyn.c:66: error: subscripted value is neither array nor pointer
    dyn.c:67: error: subscripted value is neither array nor pointer
    dyn.c:68: error: subscripted value is neither array nor pointer
    dyn.c:69: error: subscripted value is neither array nor pointer
    dyn.c:71: error: subscripted value is neither array nor pointer
    dyn.c:72: error: subscripted value is neither array nor pointer
    dyn.c:73: error: subscripted value is neither array nor pointer
    dyn.c:74: error: subscripted value is neither array nor pointer
    dyn.c:76: error: subscripted value is neither array nor pointer
    dyn.c:76: error: subscripted value is neither array nor pointer
    dyn.c:76: warning: cast to pointer from integer of different size
    dyn.c:77: error: subscripted value is neither array nor pointer
    dyn.c:78: error: subscripted value is neither array nor pointer
    dyn.c:79: error: subscripted value is neither array nor pointer
    dyn.c:80: error: subscripted value is neither array nor pointer
    Note: All the RValues (constants) assigned in the code are just for testing purposes.. these values are known dynamically when run on real time.
    Kindly please help me out :-|
    Thanks in advance.
    Btw, i referred this here as an example to implement this
    Last edited by innqubus; 07-10-2008 at 08:27 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, let's look at line 58, since that's the first error:
    Code:
    harqInfo[q]->zoneInfoInMap = (ZoneInfoInMap_t**)realloc(harqInfo[q]->zoneInfoInMap,(z+1)*sizeof(ZoneInfoInMap_t*));
    harqInfo[q]->zoneInfoInMap is a ZoneInfoInMap_t structure, which makes sense I suppose. Why you think that can hold, or should be able to hold, a pointer to a pointer to a ZoneInfoInMap_t I do not know. (Note that this does not refer to the confusingly-named variable zoneInfoInMap, which is indeed a ZoneInfoInMap_t **, which is just a plain old variable, and does not belong to the harqInfo array of structures.)

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What I believe you have going is the ability to scale your number of ZoneInfoInMap structures within your harqInfo[], but that is not what you are doing with your reallocs. What I see is that you are dynamically trying to alter the memory footprint of the ZoneInfoInMap structure which is not allowed, as that is hard set and padded to be 2 bytes ( uint8_t noOfCids + CidInfoInMat_t cidInfoInMap).

    As noted already, your zoneInfoInMap is not an array, but a structure name within your HarQAckInfo_t structure, that is why you are getting "error: subscripted value is neither array nor pointer" messages. The same goes for your cidInfoInMap and DlmAcid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  2. array of structures
    By tish in forum C Programming
    Replies: 9
    Last Post: 04-05-2009, 03:17 AM
  3. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 04:54 PM
  4. 2D array in dynamic memory
    By VanJay011379 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 07:34 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21