Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

60%
+1 −0
Q&A 2D-array pointer as a struct member

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. T...

posted 16h ago by Lundin‭  ·  edited 16h ago by Lundin‭

Answer
#2: Post edited by user avatar Lundin‭ · 2025-01-09T10:40:59Z (about 16 hours ago)
  • 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.
  • 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:
  • ```c
  • 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;
  • }
  • }
  • }
  • ```
  • 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:
  • ```c
  • 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: Initial revision by user avatar Lundin‭ · 2025-01-09T10:35:31Z (about 16 hours ago)
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. 

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:

```c
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;
    }
  }
}
```