Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
2D-array pointer as a struct member
I have an array of struct:
static struct basket baskets[MAX_ITEMLEN + 1];
struct basket {
char *items; // malloc(itemlen * itemcount)
int itemcount;
};
char *items
does all I need to do with some pointer-gymnastics, but at times it gets a little ugly.
I would prefer a 2D-array pointer allowing items[item_index][char_index]
navigation by default, but all my attempts to get a 2D-array pointer as struct member have failed miserably.
If it is indeed possible: how?
Details (in case relevant):
#1 Specs:
#define MAX_ITEMLEN 20
#define MAX_ITEMCOUNT 16000
#2 itemcount
(s) & the char content of each item are not known at compile-time but are derived at start-up by parsing two user-provided .txt files
#3 The code will not interrogate baskets[x]
having itemcount == 0
#4 *items
is initialised: |←item_chars→|←item_chars→| ...itemcount times
...with no delimiters ('\0' or otherwise)
#5 After initialsation, basket[]
is invariant until exit()
2 answers
If you don't mind the extra memory, you can do it with an extra array:
struct basket {
char *item_memory;
char **items;
int itemcount;
};
/* I omitted any error handling */
void initialize(basket *b, int itemlen, int itemcount) {
int item_index;
b->item_memory = malloc(itemlen * itemcount);
b->items = malloc(sizeof(char*) * itemcount);
b->itemcount = itemcount;
for (item_index = 0; item_index < itemcount; ++item_index)
b->items[item_index] = item_memory + itemlen ** item_index;
}
Of course here the actual storage is not in *items
but in *item_memory
. You cannot avoid that if you want to use double index on items
; the only thing in C you can apply the index operator to are pointers, therefore *index
must be a pointer.
Given that you don't need the flexibility provided by a char**
but rather need efficiency, plus a fairly large amount of items, it does sound like you need a true 2D array allocated on the heap.
The char* member isn't ideal either for performance reasons, since it will point at data allocated outside the struct array. Ideally the struct array will be allocated on the heap too, along with the pointed-at data. But if you want this to be truly flexible and realloc:able, you'll probably have to accept that the data is stored elsewhere.
The closest mechanism provided by C are flexible array members. However, these are cumbersome since they don't really support multiple dimensions. Also if we wanted to alloc the whole array on the heap this won't be a good solution since individual array items won't be realloc:able.
So what you already have with the char*
member is probably not easy to improve. Assuming you do baskets[0].items = calloc(itemlen, itemcount);
and so on.
Then you'll either have to use a "mangled" 2D array like:
baskets[i].items[x*itemcount + y]
Or otherwise cast to an array pointer, which isn't exactly prettier:
((char(*)[itemcount])baskets[i].items)[x][y] = something;
It is fine to type pun between a 2D array and a 1D array here as far as strict aliasing is concerned and alignment isn't a problem with char type either.
Using a temporary pointer as middle man is probably the most readable:
for(size_t i=0; i<n; i++)
{
char (*itemptr)[itemcount] = (char(*)[itemcount]) baskets[i].items;
for(size_t x=0; x<itemlen; x++)
{
for(size_t y=0; y<itemcount; y++)
{
itemptr[x][y] = something;
}
}
}
1 comment thread