遍历C中未知大小的结构成员的结构数组 [英] Iterate through array of structs with unknown size struct member in C
问题描述
我知道 stackoverflow.com 上有很多类似的问题,但我自己也找不到解决方案.经过一段时间的研究,即使我认为我想做的任务相对简单,我的大脑现在还是受伤了.
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.
所以我有两个结构:
struct files_t {
file_t *file;
};
struct file_t {
unsigned char *bytes;
unsigned int len;
};
- 第一个结构
files_t
包含不明数量的第二个结构file_t
的结构. - 第二个结构
file_t
包含一个unsigned char
数组,该数组表示file_t
的字节以及此bytes的长度
-array. - The first struct
files_t
contains a unknown amount of structs of the second structfile_t
. - The second struct
file_t
contains an array ofunsigned char
which represents the bytes of thefile_t
and the length of thisbytes
-array.
然后我创建一个指向 files_t
结构的指针:
Then I create myself a pointer to a files_t
struct:
files_t* files;
我现在的问题是:当文件中的file_t的数量未知时,如何遍历此 files
结构指针(伪代码:对于文件中的每个文件
)编译时,每个file_t的大小在编译时都不知道吗?
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)当(2)时,如何遍历此文件的结构指针(伪代码:
对于文件中的每个文件
)其中file_t
的数量在编译时未知,并且(3)每个file_t
的大小在未知时编译时间?
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_t
s in it is unknown at compile time and also (3) the size of eachfile_t
is unknown at compile time?
让我们从语句(3)开始:对您来说,好消息是,每个 file_t
的大小在编译时都是绝对已知的.它是将 sizeof(file_t)
来获得它.
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)
.
这不是一个坏消息:由于语句(2)是真实的(编译时未知其中的 file_t
s数量),不幸的是,对的回答(1)是:您不能.至少,您不能不做任何修改.
Not the bad new: Since statement (2) is true (the amount of file_t
s in it is unknown at compile time) unfortunately the answer to (1) is: you can't. At least, you can't without modifying something.
要知道 struct files_t
列表中有多少个项目(有一种迭代方法),您必须选择:
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:
- 向
struct files_t
添加一个包含项数的字段:
- Add a field to
struct files_t
containing the number of items:
struct files_t {
file_t *file;
unsigned int nrOfFiles;
};
您将小心地将其初始化为0,每当将文件添加到列表中时将其递增,而从其中删除文件时将其递减.您可以使用诸如 for(int i = 0; i< filesList.nrOfFiles; i ++)
之类的for循环遍历它.
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++)
.
- 设计一个哨兵值.前哨值表示这是数组的最后一个元素".例如:
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 };
在这种情况下,您可以迭代直到找到 {NULL,0}
元素.
In this case you can iterate until the { NULL, 0 }
element is found.
这篇关于遍历C中未知大小的结构成员的结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!