Need Explanation on Lvalue Assignment for the following snippet

Discussion in 'C Programming' started by Pranav, Sep 4, 2008.

  1. Pranav

    Pranav Guest

    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    const int i=6;
    int k = 9;
    int a, *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)
    Pranav, Sep 4, 2008
    #1
    1. Advertising

  2. Pranav

    Guest

    On Sep 4, 11:19 am, pete <> wrote:
    > Richard Heathfield wrote:
    > > pete said:

    >
    > >> Pranav wrote:

    > > <snip>
    > >>> const int i=6;
    > >>> int k = 9;
    > >>> int a, *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.h> and getchr() not being defined by the
    standard.
    , Sep 4, 2008
    #2
    1. Advertising

  3. On 4 Sep, 09:11, Richard Heathfield <> wrote:
    > 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, *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
    Nick Keighley, Sep 4, 2008
    #3
  4. Pranav

    Guest

    On Sep 4, 11:33 am, pete <> wrote:
    > wrote:
    >
    > > He did not mention <conio.h> and 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, 2008
    #4
  5. Pranav

    Willem Guest

    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
    Willem, Sep 4, 2008
    #5
  6. Pranav

    Guest

    On Sep 4, 2:13 pm, Richard Heathfield <> wrote:
    > said:
    >
    > > On Sep 4, 11:19 am, pete <> wrote:

    >
    > <snip>
    >
    > >> I think you got them all.

    >
    > > He did not mention <conio.h> and 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, 2008
    #6
  7. Pranav

    Pranav Guest

    Thank You All For quick response and in depth explanation.

    Pranav
    Pranav, Sep 4, 2008
    #7
  8. On 4 Sep 2008 at 11:39, wrote:
    > On Sep 4, 2:13 pm, Richard Heathfield <> wrote:
    >> said:
    >> > He did not mention <conio.h> and 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?
    Antoninus Twink, Sep 4, 2008
    #8
  9. Pranav

    Richard Guest

    Antoninus Twink <> writes:

    > On 4 Sep 2008 at 11:39, wrote:
    >> On Sep 4, 2:13 pm, Richard Heathfield <> wrote:
    >>> said:
    >>> > He did not mention <conio.h> and 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.
    Richard, Sep 5, 2008
    #9
  10. Pranav

    Richard Guest

    writes:

    > On Sep 4, 2:13 pm, Richard Heathfield <> wrote:
    >> said:
    >>
    >> > On Sep 4, 11:19 am, pete <> wrote:

    >>
    >> <snip>
    >>
    >> >> I think you got them all.

    >>
    >> > He did not mention <conio.h> and 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.
    Richard, Sep 5, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kavya
    Replies:
    9
    Views:
    493
    Dik T. Winter
    Oct 28, 2006
  2. Replies:
    11
    Views:
    683
    James Kuyper
    Sep 22, 2008
  3. Chad
    Replies:
    21
    Views:
    743
    Richard Bos
    Feb 20, 2009
  4. Julian Mehnle
    Replies:
    0
    Views:
    227
    Julian Mehnle
    Jul 17, 2003
  5. Vipul Kapadia

    Need explanation for a Javascript snippet

    Vipul Kapadia, Apr 20, 2009, in forum: Javascript
    Replies:
    6
    Views:
    125
    Gregor Kofler
    Apr 20, 2009
Loading...

Share This Page