Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

How can I create an array with struct elements in C?

I want to create an array that contains elements of a structure, each element of the struct is a boolean and when accessing each array element I want to modify the value of the structure. Structure is a global variable, when modifying the array element, I want to also modify the global structure.

typedef struct
{
    bool bool1;
    bool bool2;
    bool bool2;
} struct_bool;

struct_bool my_struct;

bool array_dummy[3] = {my_struct.bool1, my_struct.bool2, my_struct.bool3};

array_dummy[0] = true;
array_dummy[1] = true;
array_dummy[2] = false;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use pointers:

bool *array_dummy[3] = { &my_struct.bool1, &my_struct.bool2, &my_struct.bool3 };

*array_dummy[0] = true;
*array_dummy[1] = true;
*array_dummy[2] = false;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...