Incrementing an array result in lvalue required?my code is below please help me to understand
int main()
{
int a[]={10,20,30,40,50};
a=a+1 ;// i am increment an array address
a++ ;// but here its bad, here why lvalue required ?
}
Incrementing an array result in lvalue required?my code is below please help me to understand
int main()
{
int a[]={10,20,30,40,50};
a=a+1 ;// i am increment an array address
a++ ;// but here its bad, here why lvalue required ?
}
Array names are non-modifiable lvalues. You can't modify it. Therefore a = a + 1;
and a++;
both are wrong.
A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type,[...]
NOTE:
- A modifiable l-value is addressable (can be the operand of unary &) and assignable (can be the left operand of =).
- A non-modifiable l-value is addressable, but not assignable.
a=a+1// its good
a++//its bad, here why lvalue required ?
Actually both are not OK, arrays are second-class citizen in C: arrays cannot be assigned in C.
What you can do is to modify array elements:
for (size_t i = 0; sizeof a / sizeof *a; i++)
{
a[i]++;
}
I will make an attempt to explain the reason why arrays cannot be lvalues as was pointed out by previous answers. This would have been clearer before the standard introduced variable length arrays, as arrays, not their elements, are constants and constants cannot be lvalues beyond assignment.
Pointer arithmetic, without looping, is only valid for pointers to single objects. Arrays are best viewed as pointers to a set of objects, hence the reason why the elements of an array must be of the same type.
Arrays, like pointers, are just references to objects in memory. So when you are indexing an array element to use its value, think of it as pointing to the object that you are referencing hence the reason why a[0]
<=> *a
. But using the asterisk notation, what would be the equivalent of a[1]
? You would have to declare, assign a pointer to a + 1
and dereference the pointer. To illustrate what I am saying I will make use of a character string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
char* text = malloc(8);
unsigned short n;
printf("Enter a 7 character word: ");
scanf_s("%s", text);
do {
printf("Which character by 0-indexing would you like to print?: ");
scanf_s("%u", &n);
} while (n > strlen(text) - 1);
printf("Character %u is %c\n", n, text[n]);
free(text)
return 0;
}
Now, when you doing any addition or subtraction on pointers or/and arrays, what you are basically doing is moving by reference from one object to the next. Note that by pointer and array arithmetic, I am referring to this text = text + 1
which is where your question is. If an array is just a reference to a set of objects of the same type, how do you perform any form of pointer arithmetic on a whole array without a loop? That would be implying that the array as it is now making reference to a set of objects in memory that follows after the array. But the array must refer to a set of objects of the same type that is of the same as the previous set of objects. Do you realise why this is "illegal" or not possible or allowed? If you perform tests on a = a + 1
you might learn that that line of code was slipped under the rug by your compiler OR a[0] is now pointing to what a[1] is pointing to. If you write other programs that make use of such arithmetic, I would be interested in the test results. By performing such arithmetic, you are likely to end up accessing memory you are not supposed to access which causes...
Finally, a very IMPORTANT concept here that we all need to remember is that x = x + 1
<=/=> x++
for the entire set upon which these instructions are valid. x++
does not mean always add 1. It means "advance the value in a meaningful way" as in http://www.embedded.com/design/programming-languages-and-tools/4410601/Pre-increment-or-post-increment-in-C-C-. In this context the meaningful way could be advancing to the memory address immediately after the array as opposed to advancing array locations by 1.
int *rmv[];
clrscr();
printf("enter the the 10 values");
for(int arnicsc=1;arnicsc<=10;arnicsc++)
{
scanf("%d",&rmv[arnicsc]);
}
for(int arnicsc=1;arnicsc<=10;arnicsc++)
{
printf("the value of array %d=[%d]",arnicsc,++rmv[arnicsc]);
}