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
        }
    }
}   
  • 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). – CiaPan Aug 03 '15 at 12:09

2 Answers2

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)
Haris
  • 12,120
  • 6
  • 43
  • 70
  • @TheParamagneticCroissant: mistake, corrected, thanx – Haris Aug 03 '15 at 11:51
  • @TheParamagneticCroissant the answer is correct or wrong –  Aug 03 '15 at 11:52
  • the same procedure should be followed in switch statement also right.? –  Aug 03 '15 at 11:54
  • `strcmp` returns `0` *when the strings are identical*. You inverted the condition here. – Quentin Aug 03 '15 at 11:57
  • @PrajwalBhat: You cannot use strings in a switch statement without some extra work.. Switch statements should have integers only – Haris Aug 03 '15 at 12:00
  • @haris what is the work has to be done in order use string in switch statement lets say in the program i want to switch on type as shown below var[i].Type=Real means i have to do some work and var[i].Type=Integer means i have to do some other work like that –  Aug 03 '15 at 12:11
  • @PrajwalBhat: Take a look at this http://stackoverflow.com/questions/4165131/c-c-switch-for-non-integers – Haris Aug 03 '15 at 12:13
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.

alifirat
  • 2,899
  • 1
  • 17
  • 33