Skip to main content

Command Palette

Search for a command to run...

Array name is not an lvalue

Updated
1 min readView as Markdown
Array name is not an lvalue

Program 1:

#include <stdio.h>
 int main()
 {
 int a[]={10, 20, 30, 40, 50};
 int i;
 for(i=0;i<5;++i)
 printf("\n%d",a++); //a=a+1
 return 0;
 }

Program 2:

#include <stdio.h>
 int main()
 {
 int a[]={10, 20, 30, 40, 50};
 int *p = a;
 int i;
 for(i=0;i<5;++i)
 printf("\n%d",*p++);
 return 0;
 }

On first look it may look like we are doing the same operation in Program 1 and Program 2, however Program 1 results in a compile time error:

whereas Program 2 produces the following output without any errors:

Differences Between Arrays and Pointers

The pointer p is an lvalue. An lvalue denotes the term used on the lefthand side of an assignment operator. An lvalue must be capable of being modified. An array name such as a is not an lvalue and cannot be modified. The address assigned to an array cannot be changed . A pointer can be assigned a new value and reference a different section of memory.

11 views

More from this blog

P

Programming Tutorials

89 posts