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.

Comments on 2D-array pointer as a struct member

Post

2D-array pointer as a struct member

+4
−0

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()

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Are the data strings? (3 comments)
Are the data strings?
Lundin‭ wrote 1 day ago

In case the data is strings, an array of pointers char** is often the most convenient since it is flexible and memory efficient, but at the cost of execution speed. If the data is something else, then the flexibility of using a char** is probably not worth it.

wasIzy‭ wrote about 20 hours ago

Data are values that fit comfortably in a char, NOT strings. They may match a "dictionary word" in some language, but are NEVER used in any string-like context (string.h, printf...%s, etc).

Execution speed is important: the code will access baskets[] millions of times in a run, how many millions depends on the user-provided "problem definition".

BTW your SO self-answered question on correctly declaring 2D-arrays is one of the very few bookmarks tagged GOLD in my browser. If only it covered struct...

wasizy

Lundin‭ wrote about 9 hours ago

wasIzy‭ 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.