I’ve read plenty of posts about this, but it seems, that my problem is a bit more specific. I think I would manage to allocate a dynamic 2d array.
Due to existing code, I have to use a typedef which is a static array. Now I want to store an unknown number of these arrays temporarily. I’ve tried several variations with pointers, but I can’t even compile it.
Following code should explain what I’m trying to do:
int iCount, i;
typedef unsigned char Buffer[1024];
Buffer * BufferArray=NULL;
BufferArray = malloc(iCount * sizeof Buffer*);
for(i=0;i<iCount;i++)
{
BufferArray[i] = malloc(sizeof(Buffer));
}
This is my version with fewest errors. The only one left is
error C2106: ‘=’ left operand must be l-value
I know this topic is tedious and occurred often enough. Though, I’m getting quite confused with the typedef, which is already a static array. So I thought, a ** pointer is not needed here.
Appreciate any help.
If the typedef confuses you, then remove it (in your head):
So when we see
Buffer, think of it as a unsigned char array of [1024].Now you’re looking to hold onto an unknown number of these arrays. Well you’re not really using a 2D dynamic array, just a single dynamic array that happens to hold static arrays:
Now
BAis a dynamically created array you can use to temporarily holdiCountnumber of static unsigned char arrays (Buffer).