Re: Why is this not a modifiable lvalue.

Discussion in 'C Programming' started by Simon Biber, Jun 27, 2003.

  1. Simon Biber

    Simon Biber Guest

    "David Crayford" <> wrote:
    > static void exchange(void *a, void *b, size_t size) {
    >
    > <snip>
    >
    > *(((int *)a)++) = *((int *)b);
    >
    > produces the following compiler error
    >
    > Operand must be a modifiable lvalue
    >
    > The cast should take care of that, shouldn't it?


    No, the result of a cast is just a value, it does not represent
    any actual storage. The result of a cast is never an lvalue.
    This means that you can't increment, decrement or assign to it.

    When you write the ++ operator, what is it that you intend to
    increment? To increment a pointer requires that the compiler can
    know how big is the object to which it points. You can't do
    pointer arithmetic on a generic pointer.

    The following statement
    *((int *)(a = (int *)a + 1) - 1) = *((int *)b);

    1. Convert the value of `a' into a pointer to int.
    2. Add one to that pointer (point to next int, or increase the
    byte address by sizeof(int)).
    3. Assign the result back into `a', with an automatic conversion
    to the destination type (pointer to void).
    4. Convert the assigned value back into a pointer to int.
    5. Subtract one (point to previous int, or decrease the byte
    address by sizeof(int)).
    6. Dereference this pointer.

    For this to work, the function must be passed the address of
    an element in an array of int.

    --
    Simon.
    Simon Biber, Jun 27, 2003
    #1
    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. Dan Pop

    Re: Why is this not a modifiable lvalue.

    Dan Pop, Jun 27, 2003, in forum: C Programming
    Replies:
    6
    Views:
    4,887
    Dan Pop
    Jun 30, 2003
  2. Martin Ambuhl

    Re: Why is this not a modifiable lvalue.

    Martin Ambuhl, Jun 27, 2003, in forum: C Programming
    Replies:
    0
    Views:
    694
    Martin Ambuhl
    Jun 27, 2003
  3. Micah Cowan

    Re: Why is this not a modifiable lvalue.

    Micah Cowan, Jun 28, 2003, in forum: C Programming
    Replies:
    1
    Views:
    748
    CBFalconer
    Jun 28, 2003
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,756
    Smokey Grindel
    Dec 2, 2006
  5. Kavya

    lvalue -modifiable and non-modifiable

    Kavya, Nov 6, 2006, in forum: C Programming
    Replies:
    3
    Views:
    425
    Keith Thompson
    Nov 6, 2006
Loading...

Share This Page