简体   繁体   English

其成员是指针的结构体数组

[英]Array of struct whose member is a pointer

I have this C code for an embedded system where I am assigning the address of Var to an struct pointer member. 我有一个用于嵌入式系统的C代码,在该系统中,我将Var的地址分配给结构指针成员。 Problem is when I execute the code I see incorrect address instead of Var address in the structVarArray[1] . 问题是执行代码时,我在structVarArray[1]看到了错误的地址而不是Var地址。 However if I modify the structVarArray type to const then I get the right address but then I cannot modify the Boolean flag as it becomes const . 但是,如果我将structVarArray类型修改为const那么我得到了正确的地址,但是当它变为const我将无法修改Boolean标志。 Any idea? 任何想法?

UInt8 Var;
typedef struct
{
 UInt8 * ptrVar;
 Boolean flag;
}structType;

structType structVarArray[1] =
// const structType structVarArray[1] =
{
 {&Var,  FALSE}
};

void main(void)
{
 // Code using the above array
}

Not sure how you were attempting to access the structVarArray, below is a working example. 不知道您是如何尝试访问structVarArray的,下面是一个有效的示例。

#include <stdio.h>

unsigned char Var;
struct structType
{
    unsigned char * ptrVar;
    bool flag;
};

structType structVarArray[] =
{
    {&Var, false}
};

int main(void)
{
    *structVarArray[0].ptrVar = 50;

    printf("Var = %d\n", Var);  // prints 50

    structVarArray[0].flag = true;

    return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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