Connecting Tech Pros Worldwide Help | Site Map

Need Explanation on Lvalue Assignment for the following snippet

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 4th, 2008, 07:45 AM
Pranav
Guest
 
Posts: n/a
Default 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)

  #2  
Old September 4th, 2008, 08:15 AM
pete
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

Pranav wrote:
Quote:
#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.
Quote:
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
  #3  
Old September 4th, 2008, 08:15 AM
Richard Heathfield
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

pete said:
Quote:
Pranav wrote:
<snip>
Quote:
Quote:
>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
  #4  
Old September 4th, 2008, 08:25 AM
pete
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

Richard Heathfield wrote:
Quote:
pete said:
>
Quote:
>Pranav wrote:
<snip>
Quote:
Quote:
>>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
  #5  
Old September 4th, 2008, 08:25 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

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

  #6  
Old September 4th, 2008, 08:35 AM
pete
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

vippstar@gmail.com wrote:
Quote:
On Sep 4, 11:19 am, pete <pfil...@mindspring.comwrote:
Quote:
>Richard Heathfield wrote:
Quote:
>>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
  #7  
Old September 4th, 2008, 08:45 AM
Nick Keighley
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

On 4 Sep, 09:11, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
Pranav said:
>
Quote:
#include<stdio.h>
#include<conio.h>
non-standard header
Quote:
Quote:
int main()
int main(void)
is better style
Quote:
Quote:
{
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)
Quote:
Quote:
a[2] = 9;
>
Quote:
*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

  #8  
Old September 4th, 2008, 08:45 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

On Sep 4, 11:33 am, pete <pfil...@mindspring.comwrote:
Quote:
vipps...@gmail.com wrote:
>
Quote:
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.
  #9  
Old September 4th, 2008, 08:55 AM
Willem
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

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
  #10  
Old September 4th, 2008, 11:15 AM
Richard Heathfield
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

vippstar@gmail.com said:
Quote:
On Sep 4, 11:19 am, pete <pfil...@mindspring.comwrote:
<snip>
Quote:
Quote:
>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
  #11  
Old September 4th, 2008, 11:15 AM
Richard Heathfield
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

Nick Keighley said:
Quote:
On 4 Sep, 09:11, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
>Pranav said:
>>
Quote:
#include<stdio.h>
#include<conio.h>
>
non-standard header
<sighYes, I know.
Quote:
>
Quote:
Quote:
int main()
>
int main(void)
is better style
True enough.
Quote:
>
Quote:
Quote:
{
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.
Quote:
Quote:
Quote:
a[2] = 9;
>>
Quote:
*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.
Quote:
>
#include <stdio.h>
Unnecessary header (since we're being picky). :-)
Quote:
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
  #12  
Old September 4th, 2008, 11:45 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

On Sep 4, 2:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
vipps...@gmail.com said:
>
Quote:
On Sep 4, 11:19 am, pete <pfil...@mindspring.comwrote:
>
<snip>
>
Quote:
Quote:
I think you got them all.
>
Quote:
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.
  #13  
Old September 4th, 2008, 12:15 PM
Pranav
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

Thank You All For quick response and in depth explanation.

Pranav

  #14  
Old September 4th, 2008, 05:15 PM
Antoninus Twink
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

On 4 Sep 2008 at 11:39, vippstar@gmail.com wrote:
Quote:
On Sep 4, 2:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
>vipps...@gmail.com said:
Quote:
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?

  #15  
Old September 5th, 2008, 11:15 AM
Richard
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

Antoninus Twink <nospam@nospam.invalidwrites:
Quote:
On 4 Sep 2008 at 11:39, vippstar@gmail.com wrote:
Quote:
>On Sep 4, 2:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
>>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.
  #16  
Old September 5th, 2008, 10:25 PM
Richard
Guest
 
Posts: n/a
Default Re: Need Explanation on Lvalue Assignment for the following snippet

vippstar@gmail.com writes:
Quote:
On Sep 4, 2:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
>vipps...@gmail.com said:
>>
Quote:
On Sep 4, 11:19 am, pete <pfil...@mindspring.comwrote:
>>
><snip>
>>
Quote:
>I think you got them all.
>>
Quote:
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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.