0

I have created a typedef structure as shown below which consists of 4 fields and i have inserted as array of structures which can be seen below

typedef struct Signal {
    long vr;
    char name[20];
    char Type[20];
    char casuality[20];
} Signal;

I have used array of structures as shown below

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
        }
    }
}   
1
  • It's nothing about array processing and while loop, it is just that you try to assign a (const char *) value (a pointer to an anonymous char array "output") to the char [20] variable casuality in the i-th item of your var array. Same error will arise for a standalone declaration char casuality[20]; and an assignment casuality = "xxx"; Confusion of assignment = and comparision == is another problem, and yet another is 'comparing` string characters with ordinary == operator (which doesn't work for char arrays as you would expect). Commented Aug 3, 2015 at 12:09

2 Answers 2

2

Edited

Its showing error because you are assigning(i assume u meant to compare here) the base address of the array casuality to the string "output"

Another thing is you cannot use == to compare strings because it will always compare the base address of the two strings and not the contents.

So, to compare the contents, you should use strcmp(). So your while loop will look something like this will look like this.

while(strcmp(var[i].casuality, "output") == 0)
Sign up to request clarification or add additional context in comments.

7 Comments

@TheParamagneticCroissant: mistake, corrected, thanx
@TheParamagneticCroissant the answer is correct or wrong
the same procedure should be followed in switch statement also right.?
strcmp returns 0 when the strings are identical. You inverted the condition here.
@PrajwalBhat: You cannot use strings in a switch statement without some extra work.. Switch statements should have integers only
|
0

You should use the function strcmp to compare string in C and loop like this :

while (strcmp(var[i].casuality,"output") != 0)

The condition in your loop is an affectation, not a comparaison between two string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.