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

75%
+4 −0
Q&A 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 nee...

2 answers  ·  posted 2d ago by wasIzy‭  ·  last activity 7h ago by Alexei‭

Question c array
#3: Post edited by user avatar wasIzy‭ · 2025-01-08T23:31:16Z (about 18 hours ago)
  • 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:
  • ```c
  • #define MAX_ITEMLEN 20
  • #define MAX_ITEMCOUNT 16000
  • ```
  • #2 `itemcount(s)` & `item-chars` 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()`
  • 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:
  • ```c
  • #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: Post edited by user avatar Alexei‭ · 2025-01-08T08:23:06Z (1 day ago)
code formatting
  • 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?
  • wasizy
  • Details (in case relevant):
  • #1 Specs: #define MAX_ITEMLEN 20 #define MAX_ITEMCOUNT 16000
  • #2 itemcount(s) & item-chars 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()
  • 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:
  • ```c
  • #define MAX_ITEMLEN 20
  • #define MAX_ITEMCOUNT 16000
  • ```
  • #2 `itemcount(s)` & `item-chars` 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()`
#1: Initial revision by user avatar wasIzy‭ · 2025-01-07T22:20:12Z (2 days ago)
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?

wasizy




Details (in case relevant):

#1 Specs:  #define MAX_ITEMLEN 20  #define MAX_ITEMCOUNT 16000

#2 itemcount(s) & item-chars 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()
c