简体   繁体   中英

How to cache part of the data in buffer/ array and have everything else stored in members of data structure in C

I have my pseudocode something like this in C. I have some part of data stored in data structure, but im struggling to have another set of data (based on an if condition) to store in a separate array which is not fixed size. Any suggestion is appreciated.

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++;
} 

You must create a pointer, since lists in C must have a specific size

int* arr;
arr = (int*)malloc(sizeof(int)*some_number);

and then in your code

else {
        k = n;
        array[curnt_cnt] = k;
    }

will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM