-3
#include<stdio.h>
 int main(){
    char arr[] = "Pickpocketing my peace of mind";
    int i;
    printf("%c",*arr);
    arr++;
    printf("%c",*arr);

   return 0;
 }

I am getting error: lvalue required as increment operand. My question is , if we increment a variable like int i=10; i++ will not give any error. but y this?

yashu
  • 29
  • 1
  • 7

2 Answers2

2

You are trying to modify an array which is not permissible. You can't modify an array but its value. As you can see that error message itself speaks that arrays are not an lvalue so you can't modify it.
Do not confuse an array with a pointer.

char arr[] = "Pickpocketing my peace of mind"; 
char *ptr = arr;
ptr++;            // OK. ptr is an lvalue. It can be modified.
arr++;            // ERROR. arr in not an lvalue. Can't be modified.
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Hi Thank you, But my confusion is, arr will give the base address, which is the address of the first element, if I do arr++, it should point to next element address in the array right? – yashu Aug 08 '18 at 10:49
  • 1
    You can't modify an array. I made it clear in the answer. – haccks Aug 08 '18 at 10:49
0

Moving an array pointer is not permitted. If you want to move to next elements using ++ on array pointers just copy the array pointer in to some other pointer variable. Like

char arr[] = "Pickpocketing my peace of mind";
char *movablePtr = arr;

Now you can move movablePtr by movablePtr++ / movablePtr--.

Also I to add when you say arr[5] actually it means *(arr + 5 * sizeof(char)) when array is of char type.

Rizwan
  • 3,324
  • 3
  • 17
  • 38