-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?

1
  • It is illegal because someone decided that this is illegal. Commented Aug 8, 2018 at 11:17

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

2 Comments

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?
You can't modify an array. I made it clear in the answer.
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.

6 Comments

Thank you so much, Rizwan...
It'd seem like you're indicating that arrays are pointers, but they are not. They are just converted to a pointer to their first element in most contexts.
May be I was not very clear in expressing, but indeed you are right Arrays are not pointers. I explained it in this way just to give some insight.
Also arr[5] is not equivalent to what you describe, but *(arr + 5).
I think you mean *(arr + 5 * sizeof(char)) in this case.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.