473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Need Explanation on Lvalue Assignment for the following snippet

#include<stdio.h>
#include<conio.h>
int main()
{
const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;
a[2] = 9;

*c++ = *v++; // ( 1 ) This line does not generate any exception /
error
k++ = k++; // ( 2 ) But this line is generating
k++ = 6; // ( 3 ) This Also Generating

getch();
}

Now I know that you cannot assign a value to an expression; But why
does the above (1) expression not generating a exception/error/
warning? and (2) & (3) are generating as they are supposed to. Please
explain this.
Is this due to operator precedence in the case(1), ie the expression
access the value and then assign them and then increment the pointers
and move forward. Correct if I am wrong..,

(I am using DevC++ Compiler for testing)
Sep 4 '08 #1
15 1305
Pranav wrote:
#include<stdio.h>
#include<conio.h>
int main()
{
const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;
a[2] = 9;

*c++ = *v++; // ( 1 ) This line does not generate any exception /
You're assigning a value to (*c) there.
There nothing wrong with that.
error
k++ = k++; // ( 2 ) But this line is generating
k++ = 6; // ( 3 ) This Also Generating
Though (k++) has the same value
as (k) did before the increment,
(k++) is the result of an arithmetic operation.

A simpler example of the same problem, would be:

(k + 1) = 6;

(&k) is the address of (k), but (k + 1) doesn't have an address,
so there is no place to put the value of (6) into.

--
pete
Sep 4 '08 #2
pete said:
Pranav wrote:
<snip>
>const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;
a[2] = 9;

*c++ = *v++; // ( 1 ) This line does not generate any exception /

You're assigning a value to (*c) there.
There nothing wrong with that.
Look again! :-)

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 4 '08 #3
Richard Heathfield wrote:
pete said:
>Pranav wrote:
<snip>
>>const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;
a[2] = 9;

*c++ = *v++; // ( 1 ) This line does not generate any exception /
You're assigning a value to (*c) there.
There nothing wrong with that.

Look again! :-)
I was just addressing the lvalue issue from the subject line.
The code has other problems too.
I read you other post in this thread.
I think you got them all.

--
pete
Sep 4 '08 #4
On Sep 4, 11:19 am, pete <pfil...@mindspring.comwrote:
Richard Heathfield wrote:
pete said:
Pranav wrote:
<snip>
>const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;
a[2] = 9;
>*c++ = *v++; // ( 1 ) This line does not generate any exception /
You're assigning a value to (*c) there.
There nothing wrong with that.
Look again! :-)

I was just addressing the lvalue issue from the subject line.
The code has other problems too.
I read you other post in this thread.
I think you got them all.
He did not mention <conio.hand getchr() not being defined by the
standard.

Sep 4 '08 #5
vi******@gmail.com wrote:
On Sep 4, 11:19 am, pete <pfil...@mindspring.comwrote:
>Richard Heathfield wrote:
>>pete said:
Pranav wrote:
<snip>
const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;
a[2] = 9;
*c++ = *v++; // ( 1 ) This line does not generate any exception /
You're assigning a value to (*c) there.
There nothing wrong with that.
Look again! :-)
I was just addressing the lvalue issue from the subject line.
The code has other problems too.
I read you other post in this thread.
I think you got them all.

He did not mention <conio.hand getchr() not being defined by the
standard.
Even though getchr() isn't defined by the standard,
I think you mean getch().

--
pete
Sep 4 '08 #6
On 4 Sep, 09:11, Richard Heathfield <r...@see.sig.invalidwrote:
Pranav said:
#include<stdio.h>
#include<conio.h>
non-standard header
int main()
int main(void)
is better style
{
const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;
"a" appears to be a C99 VLA (older versions of C
don't support variable length arrays)
a[2] = 9;
*c++ = *v++; // ( 1 ) This line does not generate any exception /
error

Nevertheless, the behaviour of the statement, and therefore the program, is
undefined, because *v++ reads a[0], the value of which is indeterminate.
as c and v point to the same thing wouldn't there be UB even if
a[0] was initialised?

#include <stdio.h>

int main (void)
{
int a [6] = {0};
int *c = a;
int *v = a;

*c++ = *v++; /* UB I believe */
return 0;
}

<snip>

--
Nick Keighley

Sep 4 '08 #7
On Sep 4, 11:33 am, pete <pfil...@mindspring.comwrote:
vipps...@gmail.com wrote:
He did not mention <conio.hand getchr() not being defined by the
standard.

Even though getchr() isn't defined by the standard,
I think you mean getch().
That's right.
Sep 4 '08 #8
Nick Keighley wrote:
) as c and v point to the same thing wouldn't there be UB even if
) a[0] was initialised?
)
) #include <stdio.h>
)
) int main (void)
) {
) int a [6] = {0};
) int *c = a;
) int *v = a;
)
) *c++ = *v++; /* UB I believe */
) return 0;
) }

Nope, that's not UB.
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
Sep 4 '08 #9
vi******@gmail.com said:
On Sep 4, 11:19 am, pete <pfil...@mindspring.comwrote:
<snip>
>I think you got them all.

He did not mention <conio.hand getchr() not being defined by the
standard.
That's true - I didn't. Feel free to do so yourself.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 4 '08 #10
Nick Keighley said:
On 4 Sep, 09:11, Richard Heathfield <r...@see.sig.invalidwrote:
>Pranav said:
#include<stdio.h>
#include<conio.h>

non-standard header
<sighYes, I know.
>
int main()

int main(void)
is better style
True enough.
>
{
const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;

"a" appears to be a C99 VLA (older versions of C
don't support variable length arrays)
Yes, but it's still legal C, if he's using a C99 compiler, which is not
beyond the bounds of possibility. Another indication that he might be is
that he's using BCPL-style comments.
a[2] = 9;
*c++ = *v++; // ( 1 ) This line does not generate any exception /
error

Nevertheless, the behaviour of the statement, and therefore the program,
is undefined, because *v++ reads a[0], the value of which is
indeterminate.

as c and v point to the same thing wouldn't there be UB even if
a[0] was initialised?
No: ignoring the ++s for a while, it would simply be the equivalent of a[0]
= a[0]; which is perfectly legal. The ++s affect the pointers (which are
different objects), not the object to which they both point.
>
#include <stdio.h>
Unnecessary header (since we're being picky). :-)
int main (void)
{
int a [6] = {0};
int *c = a;
int *v = a;

*c++ = *v++; /* UB I believe */
You believe wrong, for the reason stated.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 4 '08 #11
On Sep 4, 2:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
vipps...@gmail.com said:
On Sep 4, 11:19 am, pete <pfil...@mindspring.comwrote:

<snip>
I think you got them all.
He did not mention <conio.hand getchr() not being defined by the
standard.

That's true - I didn't. Feel free to do so yourself.
I don't understand your reply, I did so. If my reply came out as
offensive, it was not my intention. I was not trying to correct you, I
had the genuine intentions of replying to the OP with a full post, but
after seeing yours, the only thing to add was that conio/getch is not
standard.
Sep 4 '08 #12
Thank You All For quick response and in depth explanation.

Pranav

Sep 4 '08 #13
On 4 Sep 2008 at 11:39, vi******@gmail.com wrote:
On Sep 4, 2:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>vipps...@gmail.com said:
He did not mention <conio.hand getchr() not being defined by the
standard.

That's true - I didn't. Feel free to do so yourself.

I had the genuine intentions of replying to the OP with a full post,
but after seeing yours, the only thing to add was that conio/getch is
not standard.
It's crystal clear that your intention was to be a prissy little
dickhead as usual. If the OP is writing a console program for Windows,
WHY IN THE HELL should he listen to your whining about non-"Standard"
functions THAT HE NEEDS TO USE?

Sep 4 '08 #14
Antoninus Twink <no****@nospam.invalidwrites:
On 4 Sep 2008 at 11:39, vi******@gmail.com wrote:
>On Sep 4, 2:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>>vipps...@gmail.com said:
He did not mention <conio.hand getchr() not being defined by the
standard.

That's true - I didn't. Feel free to do so yourself.

I had the genuine intentions of replying to the OP with a full post,
but after seeing yours, the only thing to add was that conio/getch is
not standard.

It's crystal clear that your intention was to be a prissy little
dickhead as usual. If the OP is writing a console program for Windows,
WHY IN THE HELL should he listen to your whining about non-"Standard"
functions THAT HE NEEDS TO USE?
It was blatantly obvious at an early stage that vippstar is only
interested in currying favour with the clique at each and every
opportunity.
Sep 5 '08 #15
vi******@gmail.com writes:
On Sep 4, 2:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>vipps...@gmail.com said:
On Sep 4, 11:19 am, pete <pfil...@mindspring.comwrote:

<snip>
>I think you got them all.
He did not mention <conio.hand getchr() not being defined by the
standard.

That's true - I didn't. Feel free to do so yourself.

I don't understand your reply, I did so. If my reply came out as
offensive, it was not my intention. I was not trying to correct you, I
had the genuine intentions of replying to the OP with a full post, but
after seeing yours, the only thing to add was that conio/getch is not
standard.
Try reading the thread before offering advice. It saves a lot of clc
clique in fighting and tedious repeated answers.
Sep 5 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Hongzheng Wang | last post by:
In K&R, they said: An object is a named region of storage; an lvalue is an expression refer to an object. How about these concept in C++? `The C++ Programming Language' has a similar...
9
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form...
14
by: Michael Ovetsky | last post by:
Consider: int g(){int b=1; return b;} int main() { int d=2; g()=d; }
9
by: junky_fellow | last post by:
Consider the following piece of code: (char *)0x100; /* I know that converting an integer to pointer type is implementation defined. But forget this for a moment */ My question is, Why the...
10
by: the_init | last post by:
Hi friends, I read about Lvalue in previous posting and Googled it but I'm not understood it completely. There is a small doubt. int a; a=20; // here a is Lvalue But
15
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
1
by: subramanian100in | last post by:
Consider the following: int x; int y; int z; (x+y) = z; For this statement, I get the following error with g++ compiler: error: non-lvalue in assignment Suppose I have a class Test and x,...
16
by: jackie | last post by:
i know that in c plus plus,++x returns l-value while x++ returns r- value,but what is the situation in c,are both ++x and x++ return r- value? i don't know how C99 defines it,thx.
11
by: markryde | last post by:
Hello, Followed here is a simplified code example of something which I try to implement; in essence , I want to assign a value to a return value of a method is C. I know, of course, that in this...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.