Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array increment operator in C

I don't understand the results of following code:

#include <stdio.h>
#include <conio.h>
int main()
{
   int a[4]={1, 3, 5, 6};
   //suppose a is stored at location 2010
   printf("%d\n", a + 2);
   printf("%d", a++);
   return 0;
}

Why does the second printf function produce following error?

error: lvalue required as increment operand

like image 638
user221458 Avatar asked Oct 02 '13 04:10

user221458


1 Answers

The name of array is a constant pointer and so it will always point to the 0th element of that array. It is not a variable so nor can we assign some other address to it neither can we move it by incrementing or decrementing. Hence

a = &var;  /*Illegal*/
a++;       /*Illegal*/
a = a-1;   /*Illegal*/
like image 196
Outlaw Avatar answered Sep 17 '22 20:09

Outlaw