我在C中有类似的伪代码。我有一些数据存储在数据结构中,但我很难将另一组数据(基于if条件(存储在一个不固定大小的单独数组中。欢迎提出任何建议。
typedef struct struct1 {
uint32 member1
} PACKED struct1_t
typedef struct struct2 {
struct1_t *member2
} PACKED struct2_t
uint32 curnt_cnt = 0;
for (i=0; i<some_number; i++){
if (cond) {
k = m;
struct2_t->member2[curnt_cnt].member1 = k; #I have no prob writing here
}
else {
k = n;
array[curnt_cnt] = k; ==> Is this even correct implementation?
# I want to store/ book-keep the values of k in an array throughout every iteration of for loop without overwriting the previous value
# Size of the array will not exceed "some_number (mentioned in for loop)" at any time
}
curnt_cnt++;
}
您必须创建一个指针,因为C中的列表必须具有特定大小的
int* arr;
arr = (int*)malloc(sizeof(int)*some_number);
然后在你的代码
else {
k = n;
array[curnt_cnt] = k;
}
将起作用。