casting and ++ operators !!

D

duggydiggy

int main()
{
char arr[]={1,2,3,4};

char *c=arr;
*c++='x'; // works fine
*(c+1)++='9'; // l-value needed case 1
void *ptr=arr;
*((char *)ptr+1)=8; // works fine
*((char *)ptr)++=8; //l-value needed case 2
*((char *)ptr+1)++=8;//l-value needed case 3
return 0;
}

1.Can somebody explain the reasons behind these specially case 1 and
case 2
2.Is the reason for case 1 and case 3 same,means compilers using
intermediate objects for offset calculations,and I am applying ++ on
that object,not the use of casting operator
3.Cast operators and ++ operator return a non l-value objects,what
does this mean?

Thanks in advance.

Cheers!!
 
M

Mark Bluemel

int main()
{
   char arr[]={1,2,3,4};
   char *c=arr;
   *c++='x';       // works fine
   *(c+1)++='9';  // l-value needed case 1 void *ptr=arr;
   *((char *)ptr+1)=8;  // works fine
   *((char *)ptr)++=8;  //l-value needed case 2 *((char
   *)ptr+1)++=8;//l-value needed case 3 return 0;
}
1.Can somebody explain the reasons behind these specially case 1 and
case 2
2.Is the reason for case 1 and case 3 same,means compilers using
intermediate objects for offset calculations,and I am applying ++ on
that object,not the use of casting operator 3.Cast operators and ++
operator return a non l-value objects,what does this mean?

By looking more closely to your post, I think that your confusion comes
from the fact that you have not fully understood the difference between
(a+1) and (a++).

(a+1) evaluates a, adds 1 to it, and returns it as the value of the whole
expression.

(a++) evaluates a, adds 1 to it, *stores back the result in a*, and
returns it as the value of the whole expression.

I think you meant "returns the original value of a as the value of the
whole expression"...
 
W

Willem

duggydiggy wrote:
) int main()
) {
) char arr[]={1,2,3,4};
)
) char *c=arr;
) *c++='x'; // works fine
) *(c+1)++='9'; // l-value needed case 1
) void *ptr=arr;
) *((char *)ptr+1)=8; // works fine
) *((char *)ptr)++=8; //l-value needed case 2
) *((char *)ptr+1)++=8;//l-value needed case 3
) return 0;
) }
)
) 1.Can somebody explain the reasons behind these specially case 1 and
) case 2

The ++ operator binds stronger than the * operator.
So you're doing: *((c+1)++) = '9';

And this bit: (c+1)++ is giving the error.

) 2.Is the reason for case 1 and case 3 same,means compilers using
) intermediate objects for offset calculations,and I am applying ++ on
) that object,not the use of casting operator

The reason for case 2 is the same as well. Casting also creates an
intermediate value.

) 3.Cast operators and ++ operator return a non l-value objects,what
) does this mean?

It is the ++ operator itself that is doing the complaining, not the
assignment. And it is the (c+1) that is returning the non-lvalue.

An lvalue is basically anything that can occur on the left side of an
assignment. The 'l' stands for 'left'.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top