I am confused about working of the below statement:
*ptr++->str
First ++
operator is applied to ptr
which returns rvalue. Next operator is ->
has to be applied. Doesn't ->
operator require lvalue?
I am confused about working of the below statement:
*ptr++->str
First ++
operator is applied to ptr
which returns rvalue. Next operator is ->
has to be applied. Doesn't ->
operator require lvalue?
Doesnt
->
operator require lvalue?
No. See section 6.5.2.3 of the C99 standard:
The first operand of the
->
operator shall have type ‘‘pointer to qualified or unqualified structure’’ or ‘‘pointer to qualified or unqualified union’’, and the second operand shall name a member of the type pointed to....
A postfix expression followed by the
->
operator and an identifier designates a member of a structure or union object. The value is that of the named member of the object to which the first expression points, and is an lvalue.
And that's all it says on the matter.
This could be a possible definition for the struct.
#include <stdio.h>
struct fuzz {
char *str;
} stuff = {"Hi there!"} ;
struct fuzz *ptr = &stuff;
int main()
{
char ch='@';
printf("Before: p=%p, ch=%c\n", (void*) ptr, ch);
ch = *ptr++->str;
printf("After: p=%p, ch=%c\n", (void*) ptr, ch);
return 0;
}
Output:
Before: p=0x601020, ch=@
After: p=0x601028, ch=H
And the output proves that the pointer expression is an lvalue. Modifyable, if str would not point to a string constant.