Question: Which Pointer Expressions in C Programming are called as L-Values?
Answer: We already know that lvalue refers to some location or storage used by some data object, for example: variables, arrays, structures, pointers etc. Further, ‘l’ in lvalue stands for left side of assignment “=” operator meaning that left side of assignment must be an expression which results in some location in memory. Let’s take a few examples:
int index = 0; /* index, an integer, is initialized with 0 */
char address[100]; /* address, an array of 100 characters *
float avg_rain; /* avg_rain has some garbage */
float temperature;
float *fp = &avg_rain;
/*
* *fp, a pointer-to-float, is initialized with address of float
* ‘avg_rain’
*/
All the above data objects are stored in memory at specified locations. Their addresses can be obtained by using unary operator called addressof operator ‘&’. Addressof operator returns address of first byte of storage allocated to the data object.
Now, see, if constants are allocated to specified locations, can we determine their addresses using addressof operator? We take some examples:
500;
45.0; /* all these are constants *
‘A’;
All the above constants are stored in memory but where? We can’t know this? Actually, constants don’t have lvalues. And that’s why we can’t assign them values! Let’s try to find their addresses:
/*
* determine_add_const.c — Program shows if we can determine address of
* constants
*/
#include
int main(void)
{
500; /* integer constant */
45.06; /* float constant */
‘A’; /* character constant */
printf(“Address of integer ‘500’ is %p\n”, &(500));
printf(“Address of float ‘45.06’ is %p\n”, &(45.06));
printf(“Address of character \’A\’ is %p\n”, &(‘A’));
return 0;
}
Output as below:
[root@breldigital.com ch06_Pointers]# gcc determine_add_const.c
determine_add_const.c: In function ‘main’:
determine_add_const.c:9:43: error: lvalue required as unary ‘&’ operand
determine_add_const.c:10:43: error: lvalue required as unary ‘&’ operand
determine_add_const.c:11:45: error: lvalue required as unary ‘&’ operand