1

what does dereference operator return? l-value or r-value based on below example as the increment opearator is working properly. Need an explanation for the same

// venukant
#include<stdio.h>
int main()
{
  char arr[] = "gendafool";
  char *p = arr;
  ++*p; // evaluated as ++(*p) but ++ requires a l-value 
  printf(" %c", *p); // prints h

  return 0;
}
Optimus Prime
  • 69
  • 2
  • 13

2 Answers2

0

The unary * produces an lvalue as its result. A non-null pointer p always points to an object, so *p is an lvalue. For example:

int a[N];
int *p = a;
...
*p = 3;     // ok

Although the result is an lvalue, the operand can be an rvalue, as in:

*(p + 1) = 4;    // ok

Hope this answers your query.

Paul
  • 448
  • 1
  • 6
  • 14
0

From the 1999 C standard, Section 6.3.2.1, para 1 starts with .....

An lvalue is an expression with an object type or an incomplete type other than void;

and then refers to footnote 53, which says

The name ‘‘lvalue’’ comes originally from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an object ‘‘locator value’’. What is sometimes called ‘‘rvalue’’ is in this International Standard described as the ‘‘value of an expression’’.

An obvious example of an lvalue is an identifier of an object. As a further example, if E is a unary expression that is a pointer to an object, *E is an lvalue that designates the object to which E points.

In n1548 (draft of the 2011 standard), the same para starts with something worded slightly differently, but with essentially the same meaning;

An lvalue is an expression (with an object type other than void )that potentially designates an object;

This para refers to footnote 64, which is identical in content to footnote 53 from the 1999 C standard.

Bear in mind that footnotes in the standard are not normative - their purpose is to clarify and explain.

To answer the original question, the dereference operator (*p) produces an lvalue. However, the value of the expression can be used as an rvalue.

Peter
  • 35,646
  • 4
  • 32
  • 74