问题
I know that there are a many similar questions out there on stackoverflow.com but I couldn't figure out a solution for myself. After quite some time on research, my brain is broken now even though I think the task I want to do is relatively simple.
So I have two structs:
struct files_t {
file_t *file;
};
struct file_t {
unsigned char *bytes;
unsigned int len;
};
- The first struct
files_tcontains a unknown amount of structs of the second structfile_t. - The second struct
file_tcontains an array ofunsigned charwhich represents the bytes of thefile_tand the length of thisbytes-array.
Then I create myself a pointer to a files_t struct:
files_t* files;
My question now is: How can I iterate through this files struct pointer (Pseudocode: for each file in files) when the amount of file_t's in it is unknown at compile time and also the size of each file_t is unknown at compile time?
回答1:
My question now is: (1) How can I iterate through this files struct pointer (Pseudocode:
for each file in files) when (2) the amount offile_ts in it is unknown at compile time and also (3) the size of eachfile_tis unknown at compile time?
Let's start from statement (3): the good news, for you, is that the size of each file_t is absolutely known at compile time. It will be the size of a char * added to the size of an integer, and you can obtain that whenever you need it with sizeof(file_t).
Not the bad new: Since statement (2) is true (the amount of file_ts in it is unknown at compile time) unfortunately the answer to (1) is: you can't. At least, you can't without modifying something.
In order to know how many items there are in your struct files_t list (that is having a way to iterate through it) you have to options:
- Add a field to
struct files_tcontaining the number of items:
struct files_t {
file_t *file;
unsigned int nrOfFiles;
};
You will take care to initialize it to 0, to increment it whenever you add a file to the list and to decrement it whenever you remove a file from it. You can iterate through it with a for loop with something like for(int i=0; i<filesList.nrOfFiles; i++).
- Design a sentinel value. A sentinel value is something meaning "this is the last element of the array". For example:
struct files_t {
file_t *file;
};
struct file_t {
unsigned char *bytes;
unsigned int len;
};
struct file_t fileArray =
{
{ charPointer0, len01 },
{ charPointer1, len1 },
/* ... */
{ charPointerN, lenN },
/* Sentinel */
{ NULL, 0 }
};
struct files_t fileList = { fileArray };
In this case you can iterate until the { NULL, 0 } element is found.
回答2:
The problem is not at compile time but at execution time, almost all is always unknown at compile time.
Your alone problem concerns the unknown numbers of elements in your arrays, if you have no way to have these numbers you can mark the end of each :
- the last
file_tcan have its fieldbytesvaluing NULL. I use the pointer rather thanlento allow you to manage empty files - the last pointer in the array of
files_tcan be NULL
来源:https://stackoverflow.com/questions/62005230/iterate-through-array-of-structs-with-unknown-size-struct-member-in-c