我创建了一个 typedef 结构,如下所示,它由 4 个字段组成 我已经插入了结构数组,如下所示
typedef struct Signal {
long vr;
char name[20];
char Type[20];
char casuality[20];
} Signal;
我使用了如下所示的结构数组
void xmlRead()
{
int i;
Signal var[4];
var[0].vr=1073741824;
strcpy(var[0].name,"Ball1_pos");
strcpy(var[0].Type,"Real");
strcpy(var[0].casuality,"output");
var[1].vr=1073741825;
strcpy(var[1].name,"Ball2_pos");
strcpy(var[1].Type,"Real");
strcpy(var[1].casuality,"output");
var[2].vr=1073741826;
strcpy(var[2].name,"Ball1_vel");
strcpy(var[2].Type,"Real");
strcpy(var[2].casuality,"output");
var[3].vr=1073741827;
strcpy(var[3].name,"Ball2_vel");
strcpy(var[3].Type,"Real");
strcpy(var[3].casuality,"output");
for(i=0; i<=3; i++)
{
while(var[i].casuality="output") **//Here it is showing error as expression must have modifiable lvalue//**
{
//Some statements
}
}
}
最佳答案
已编辑
它显示错误是因为您正在将数组 casuality
的基址分配(我假设您打算在这里进行比较)到字符串 "output"
另一件事是你不能使用 ==
来比较字符串,因为它总是比较两个字符串的基地址而不是内容。
因此,要比较内容,您应该使用strcmp()
。所以你的 while 循环看起来像这样。
while(strcmp(var[i].casuality, "output") == 0)
关于c - 如何在while循环中使用结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31786439/