90 views

Inside a program loop

if we write

*ch1++

it will give "lvalue required" error

but without any loop if we write even

++*ch1++;

it will not give any lvalue error. Why?

edited | 90 views
0
++*ch1++ = ++(*(ch1++)) which is equal to *(ch1++)= (*(ch1++))+ 1
*ch1++ here lvalue is not present
0
0
no..
0
what ???
0
that is not my query. Read question carefully
0
#include <stdio.h>

int main()
{  int c=10, *ch=&c, i=10;
*ch++;
while(i--){
*ch++;
}
printf("Hello, World!\n%d", *ch++);

return 0;
}

This code isnt giving lvalue required error. I executed it on coding ground as well as online gdp. Isn't it what you were asking for?

0
yes, that I know, it will not give lvalue error

But put *ch++ inside while loop. then it will give lvalue error.

Why so? that was my question
0
well in my case, its not giving any or probably I didnt get you. Did you mean putting it inside the condition part of while?
0
yes, in the condition part

means replace while(i--) with while(*ch++) and check
0
int main()
{  int c=10, *ch=&c, i=10;
int count=0;
while(*ch++){

printf("hey\n");
if(count++==3)break;
}

return 0;
}


No error at all. its printing hey 4 times as expected :/

0
:O
but it is giving lvalue
#include <stdio.h>

int main(void) {
char ch1[5]="GATE";
char *ptr-ch1;
while(*ch1++)
{
printf("%c",*ch1);
}
return 0;
}
+2
srestha, ch1 is a constant pointer, you cant change its value -_-
0
yes , that I know

But why giving lvalue?
+2
Because its treating ch1 as a constant, and you need to have a variable on the left hand side so that a value can be assigned to it. So if you write a constant on LHS you get an Lvalue required error.