0

Earlier posts have described why array names cannot be incremented. But.. What about these two different ways of incrementing?

void f(int arr[]){
  printf("%d\n",*arr++);
  printf("%d\n",*arr);
}

int main(void){
  int arr[3]={1,2,3};
  f(arr);
  // COMPILE BY COMMENTING THESE TWO LINES BELOW AND ADDING THEM
  printf("%d\n",*arr++);
  printf("%d\n",*arr);
}

I am not able to figure why it works in one case and not in the other. Any suggestions/guidance welcome.

giribal
  • 19
  • 3
  • 1
    you can see this question/answer https://stackoverflow.com/questions/16748150/difference-between-int-array-and-int-array-in-a-function-parameter – Max Jan 22 '19 at 07:55
  • @Max that's different, has no bearing to the question here. – Antti Haapala -- Слава Україні Jan 22 '19 at 08:02
  • @AnttiHaapala I think he has something to do with the question. – Max Jan 22 '19 at 08:18
  • I understand the concept of compiler providing arrays as a syntactic sugar. What I was asking is as far as the feature of arrays is concerned this is not a consistent behaviour. Is it documented anywhere in the C language specification? If not then is it a bug? Should it be reported? – giribal Jan 22 '19 at 08:56

1 Answers1

4

Because void f(int arr[]) is just syntactic sugar for void f(int *arr).

Inside the f function, arr ist just a pointer and not an array.

Read this SO article for more information.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115