我正在处理的代码中有很多结构,由于某种原因,结构成员的值没有得到更新。我挠挠了很长时间,但找不到原因。我在这里创建了类似的代码,而最后一行没有做任何事情。
star->state.county.street->pondType = TYPE_SQUARE
池塘类型保持为“ TYPE_NONE”。有人可以解释我在做什么错吗?。谢谢!
int main()
{
#include <stdint.h>
#include <stdbool.h>
typedef enum
{
TYPE_NONE = 0,
TYPE_SQUARE = 1,
TYPE_ROUND = 2,
} PondType_t;
typedef struct
{
uint16_t houseType;
PondType_t pondType;
} Street_t;
typedef struct
{
volatile Street_t *street;
} County_t;
typedef enum
{
IDX_1 = 0,
IDX_2 = 1,
IDX_3 = 2,
MAX_ID_COUNT = 3
} CompanyID_t;
typedef union
{
County_t county;
} CompanyState_t;
typedef struct Company_s
{
const CompanyID_t id;
CompanyState_t state;
} Company_t;
Company_t companies[MAX_ID_COUNT] =
{
{ .id = IDX_1 },
{ .id = IDX_2 },
{ .id = IDX_3 },
};
Company_t* star = &companies[IDX_1];
star->state.county.street->pondType = TYPE_SQUARE;
return 0;
}
If this is the complete code, than your
street
member points to some random area of memory. Make sure that the target that the pointer points to actually exists. Something like: