char a[] = \"hello\";
My understanding is that a
acts like a constant pointer to a string. I know writing a++
won\'t work
It is never legal in C to assign to an expression of array type. Increment (++
) involves assignment, and is thus also not legal.
What you showed at the top is a special syntax for initializing a char
array variable.
I think this answer here explains "why" it's not a good idea;
It's because array is treated as a constant pointer in the function it is declared. There is a reason for it. Array variable is supposed to point to the first element of the array or first memory instance of the block of the contiguous memory locations in which it is stored. So, if we will have the liberty to to change(increment or decrement ) the array pointer, it won't point to the first memory location of the block. Thus it will loose it's purpose.
a++
is not well-formed since a
decays to a pointer, and the result of the decay is not an lvalue (so there is no persistent object whose state could be "incremented").
If you want to manipulate pointers to the array, you should first create such a pointer:
char* p = a; // decayed pointer initializes p
a++; // OK
++a; // even OKer
This is a very good question actually. Before discussing this, let's back to the basic concepts. What happens when we declare a variable ?
int a=10;
Well, we get a memory location to store the variable a
. Apart from this an entry is created into Symbol table that contains the address of the variable and the name of the memory location (a
in this case).
Once the entry is created, you can never change anything into the symbol table, means you can't update the address. Getting an address for a variable is not in our hand, it's done by our computer system.
Let's say, we get address 400
for our variable a
.
Now computer has assigned an address for the variable a
, so at a later point, we can't ask computer to change this address 400
because again, it's not in our hand, our computer system does it.
Now you have an idea about what happens when we declare a variable.let's come to our question.
Let's declare an array.
int arr[10]
So, when we declare this array, we create the entry into the symbol table and, store the address and the name of the array into the symbol table.
let's assume we get address 500
for this variable.
Let's see what happens when we want to do something like this :
arr++
when we increment arr, we want to increment 500
, that is not possible and not in our hand, because it has been decided by the computer system, so we can't change it.
instead of doing this we can declare a pointer variable
int * p= &arr;
What happens in this situation is: again an entry is created into the symbol table that stores the name and the address of the pointer variable p
.
So when we try to increment p
by doing p++
, we are not changing the value into the symbol table, instead we are changing the value of the address of the pointer variable, that we can do and we are allowed to do.
Also it's very obvious that if we will increment the a
the ultimately we are going to loss the address of our array. if we loss the address of array then how will we access the array at a later point ?
No, it's not OK to increment an array. Although arrays are freely convertible to pointers, they are not pointers. Therefore, writing a++
will trigger an error.
However, writing
char *p = a;
p++;
is fine, becuase p
is a pointer, with value equal to the location of a
's initial element.