(Non-lvalue in assignment)

Discussion in 'Programmer's Corner' started by sall, Jul 13, 2011.

  1. sall

    Thread Starter New Member

    Jul 13, 2011
    in the following piece of code i want to assign memory to pointer ptr+1 using malloc funcn.

    {char *ptr;
    ptr ="hello";
    ptr+1 = (char *)malloc(sizeof(char));

    but compiler shows error (Non-lvalue in assignment). what does this error mean?how can i assign memory to pointer to letter e in "hello" i.e. ptr+1 so that i could free that pointer to omit the letter e from "hello" to print "hllo"?
  2. stahta01


    Jun 9, 2011
    lvalue means a value/variable that is valid on the left side of an assignment operation. Also, a valid lvalue can change its contents.

    What you are trying to do seems to be not very wise to do.

    The newer the compiler is; the more likely it will NOT allow you to over write a constant string ("hello").

    Edit: To assign a pointer to an character is not possible; an character is a single byte in size while a pointer is multiple bytes in size.

    Edit: I reread what you wanted to do.
    I can think no assignment operation that does what you want. Memory move would do it.

    The following almost does what you want; but not how you want to do it.
    Code ( (Unknown Language)):
    2. {char *ptr;
    3. ptr ="hello";
    4. printf("%c%s", ptr, ptr+2);
    5. }
    Tim S.
    Last edited: Jul 14, 2011