我有这个结构
typedef struct
{
int numberPipes; // |
int numberAmpersands; // &
int existsBiggerThan; // >
int existsLessThan; // <
int existsDoubleLargerThan; // >>
} lineData;
然后我在 char
数组 (char*) 上循环运行,以便找到 '&'
和 '|'< 的所有索引
。
我不知道要找到多少 '&'
和 '|'
。是否可以从两个数组开始(使用malloc
)
大小为 1 并在每次迭代后将它们放大,只有当我发现其中一个再次出现时?
最佳答案
您可以通过调用 realloc
来做到这一点.
出于性能原因,最好不要为每个新元素调用它,而是在 X 个元素中调用一次,这样可以减少对 malloc
/realloc
的调用次数>.
关于c - 如果满足条件,每次迭代都可以扩大数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10665904/