Tim
Tim

Reputation: 99428

How can a string literal be used as other lvalues?

A string literal is an lvalue.

An lvalue can be used:

  1. as the operand of the address-of operator (except if the lvalue designates a bit field or was declared register).

  2. as the operand of the pre/post increment and decrement operators.

  3. as the left-hand operand of the member access (dot) operator.

  4. as the left-hand operand of the assignment and compound assignment operators.

  5. as ... (anything which a value can be used as but I miss to list)

I was wondering how a string literal can be used as operands of the above operators in a C program?

Thanks.

Upvotes: 0

Views: 75

Answers (1)

M.M
M.M

Reputation: 141618

Your bullets 2 and 4 require a modifiable lvalue , which excludes arrays. String literals are arrays, so they are not modifiable lvalues.

The first one is OK, &"hello" is allowed, although this would be an uncommon usage.

The third one, the left-hand side of . must have struct type, which a string literal doesn't. However, note that the left-hand operand of . does not actually need to be an lvalue.

Upvotes: 2

Related Questions