Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment operator on pointer of array errors?

I'm trying something very simple, well supposed to be simple but it somehow is messing with me...

I am trying to understand the effect of ++ on arrays when treated as pointers and pointers when treated as arrays.

So,

int main()
{
    int a[4] = { 1, 4, 7, 9 };
    *a = 3;
    *(a+1) = 4;
    *++a = 4; //compiler error
}

1: So at *(a+1)=4 we set a[1]=4; //Happy But when *++a = 4;, I'd expect pointer a to be incremented one since ++ is precedent to * and then * kicks in and we make it equal to 4. But this code just does not work... Why is that?

Another problem:

int main()
{

    int* p = (int *)malloc(8);
    *p = 5;
    printf("%d", p[0]);

    *++p = 9; //now this works!
    printf("%d", p[1]); //garbage
    printf("%d", p[0]); //prints 9

}

2: Now *++p = 9; works fine but it's not really behaving like an array. How are two different? This is just incrementing p, and making it equal to 9. If I print p[0], it now prints 9 and I see that though can't access it via p[0] anymore, *(p-1) shows 5 is still there. So indexing a pointer with [0], where exactly does it point to? What has changed?

Thanks a lot all experts!

like image 794
shadowyman Avatar asked Oct 13 '13 06:10

shadowyman


People also ask

Can you increment an array pointer?

Pointers are also useful while working with arrays, because we can use the pointer instead of an index of the array. A pointer can be incremented by value or by address based on the pointer data type.

Why can't we increment an array like a pointer?

It's because array is treated as a constant pointer in the function it is declared.

Can you increment an array?

To increment a value in an array, you can use the addition assignment (+=) operator, e.g. arr[0] += 1 . The operator adds the value of the right operand to the array element at the specific index and assigns the result to the element.

Can we increment function pointer?

When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer. For Example: If an integer pointer that stores address 1000 is incremented, then it will increment by 2(size of an int) and the new address it will points to 1002.


2 Answers

The array names is not modifiable lvalue so operation ++ is not applied hence ++a that try to modify a is compilation time error (where a is array name).

Note *(a + 1) and *a++ are not same, a + 1 is a valid instruction as it just add 1 but doesn't modify a itself, Whereas ++a (that is equvilent to a = a + 1) try to modify a hence error.

Note 'array names' are not pointer. Pointers are variable but array names are not. Of-course when you assign array name to a pointer then in most expressions array names decays into address of first element. e.g.

int *p = a;

Note p points to first element of array (a[0]).

Read some exceptions where array name not decaying into a pointer to first element?

An expression a[i] is equivalent to *(a + i), where a can be either a pointer or an array name. Hence in your second example p[i] is valid expression.

Additionally, *++p is valid because because p is a pointer (a variable) in second code example.

like image 112
Grijesh Chauhan Avatar answered Nov 01 '22 17:11

Grijesh Chauhan


int a[4] = { 1, 4, 7, 9 };
int *pa=a;

There is one difference between an array name and a pointer that must be kept in mind. A pointer is a variable, sopa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal


int* p = (int *)malloc(8);

Don't cast result of malloc()

Use index with pointer

p[1]=9; // p[1]==*(p+1)
like image 21
Gangadhar Avatar answered Nov 01 '22 16:11

Gangadhar