3
int *insertZeroPosition(int *pf,int n,int k){
    int *ptr=(int *)calloc(n+1,sizeof(int));
    int i;
    ptr[0]=k;
    for (i=1;i<n+1;i++,pf++){
        ptr[i]=*pf; 
    }
    return ptr;
}

Why is it ptr[i]=*pf instead of *ptr[i]=*pf even though ptr is a pointer?

Woodrow Barlow
  • 8,477
  • 3
  • 48
  • 86
  • What is a _pointerunction_? – Jabberwocky Dec 05 '19 at 17:39
  • 2
    `ptr[x]` is equivalent to `*(ptr+x)`. This is really a basic syntax question. – Eugene Sh. Dec 05 '19 at 17:40
  • Does this answer your question? [What is the difference between char s\[\] and char \*s?](https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s) – dtell Dec 05 '19 at 17:41
  • I'm sorry about the word 'pointerunction'. It is a typing error. I meant to say pointer. – Rizwan Mohamed Kareem Dec 05 '19 at 17:43
  • @EugeneSh. Why this is equivalent? I´ve thought with `ptr[x]` you address the value of a certain pointer of a pointer array (which is the address stored inside that pointer variable) and with `*(ptr+x)` the value this pointer points to. – RobertS supports Monica Cellio Dec 05 '19 at 17:53
  • 2
    Because `ptr[i]` is *exactly* the same as `*(ptr+i)`, you can also say `i[ptr]` and kind of freak people out :-) – Steve Friedl Dec 05 '19 at 17:54
  • @SteveFriedl Why it is the exact same? I´ve thought with `ptr[i]` you address the value of a certain pointer of a pointer array (which is the address stored inside that pointer variable) and with `*(ptr+i)` the value this pointer points to. – RobertS supports Monica Cellio Dec 05 '19 at 17:57
  • `ptr` is not "pointer array". It is a pointer. Anyway, you have a standard quote down there. – Eugene Sh. Dec 05 '19 at 17:59
  • 2
    @RobertS-ReinstateMonica they are the same because the language says so. Arrays are not first-class data types in C - though they are in other languages - and I'm not sure how to explain it more than that. C *defined* array access notation in terms of derference-pointer plus index. Now it's true that `ptr[i]` and `(ptr + i)` are not the same thing - the first accesses the element at the array, while the second only computes the address, but adding the deference `*(ptr + i)` accesses the element. – Steve Friedl Dec 05 '19 at 18:00
  • @SteveFriedl Ok, i understand now what the clue is, so that it works for one-single pointer. But what if we would talk about as `ptr` as an array of pointers, then the expression `ptr[i]` should access the value of one pointer inside the pointer array and not the value pointed to by the first pointer, isn´t it? Does this "rule" only works for single pointers? – RobertS supports Monica Cellio Dec 05 '19 at 18:14
  • @RobertS-ReinstateMonica Pointers don't generally know if they point to a single item or an array of them, so if you have `int **ptr` then `ptr` is a pointer to a pointer, `ptr[0]` is a pointer and `ptr[0][0]` is an int. You can't tell from here if either of the pointers points to just one of a thing or more than one of a thing. But `p[1][3]` is exactly the same as `*(*(p + 1) + 3)` – Steve Friedl Dec 05 '19 at 18:26
  • please don't downvote this question just because it is a matter of simple syntax. this is a valid question for stackoverflow, with a valid answer. – Woodrow Barlow Dec 05 '19 at 20:46

1 Answers1

5

The syntax p[i] is equivalent to *((p) + (i)). The dereference is still there, even with the array-subscript syntax.

C Standard, § 6.5.2.1.2:

The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).

You can rewrite the code to say *(ptr + i) = *pf if you want; there's no difference.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85