pointer array when using scanf()

Viewed 109

In my code, I used pointer array like this.

arr = (int*)malloc(N * sizeof(int));

        for (i = 0; i < N; i++)
            scanf("%d", (arr + i));

Because pointer arithmetic points address.

But I wonder if it's right to only write (arr+i) without * or &.

What is right?

Please help me.

1 Answers

The expression arr + i has the type int * and is equivalent to the expression &arr[i] (or even to the expression &i[arr] :)).

So you may write either

scanf( "%d", &arr[i] );

or

scanf( "%d", arr + i );
Related