allocating inner struct array

S

Serve Laurijssen

Given this structure:

typedef struct S
{
struct Inner {
char desc[128];
char *file[MAX_PATH+1];
} *libs;
} S;

The Inner struct has a description string and an array of file strings. I
know the the max length of each file upfront, but not how many files go in
there. As you can see file is now declared as an array of MAX_PATH+1 char
pointers, but I want a dynamic array of static char array. (pfff)
How do I declare and assign to that?
 
B

Bertrand Mollinier Toublet

Serve said:
Given this structure:

typedef struct S
{
struct Inner {
char desc[128];
char *file[MAX_PATH+1];
} *libs;
} S;

The Inner struct has a description string and an array of file strings. I
know the the max length of each file upfront, but not how many files go in
there. As you can see file is now declared as an array of MAX_PATH+1 char
pointers, but I want a dynamic array of static char array. (pfff)
How do I declare and assign to that?
As far as declaration, what about

char (*file)[MAX_PATH + 1];

When you find out how many strings you are going to want to store, you
can use:

file = malloc(sizeof *file); /* easy enough */

and to copy your data, presumably stored filename:

char *filename;

/* fill filename out by whatever means */

strcpy(file[2], filename);

Voila !
 
S

Serve Laurijssen

Bertrand Mollinier Toublet said:
As far as declaration, what about

char (*file)[MAX_PATH + 1];

Ok, that wasn't so bad. I actually thought of this before, but in my mind I
translated it into "a pointer to a MAX_PATH+1 array" and I thought that was
wrong.
 
D

Dan Pop

Bertrand Mollinier Toublet said:
As far as declaration, what about

char (*file)[MAX_PATH + 1];

Ok, that wasn't so bad. I actually thought of this before, but in my mind I
translated it into "a pointer to a MAX_PATH+1 array" and I thought that was
wrong.

Well, this is exactly what it is: a pointer to an array of MAX_PATH + 1
char.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top