(Non-lvalue in assignment)

Thread Starter

sall

Joined Jul 13, 2011
10
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"?
 

stahta01

Joined Jun 9, 2011
133
but compiler shows error (Non-lvalue in assignment). what does this error mean?
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.
Rich (BB code):
{char *ptr;
ptr ="hello";
printf("%c%s", ptr, ptr+2);
}
Tim S.
 
Last edited:
Top