Home > Net >  Difference between arr and &arr if arr is an array of ints
Difference between arr and &arr if arr is an array of ints

Time:09-17

I am just starting out with C and I have a question. If arr is an array or 10 ints, then arr is a pointer to the first element in the array. But what is meant by &arr? SO what is the difference bwteen a pointer to an array and a reference to an array?

CodePudding user response:

then arr is a pointer to the first element in the array

No!

Arrays and pointers are two different types in C that have enough similarities between them to create confusion if they are not understood properly. The fact that pointers are one of the most difficult concepts for beginners to grasp doesn't help either.

So I fell a quick crash course is needed.

Crash course

Arrays, easy

int a[3] = {2, 3, 4};

This creates an array named a that contains 3 elements.

Arrays have defined the array subscript operator:

a[i]

evaluates to the i'th element of the array.

Pointers, easy

int val = 24;
int* p = &val;

p is a pointer pointing to the address of the object val.

Pointers have the indirection (dereference) operator defined:

*p

evaluates to the value of the object pointed by p.

Pointers, acting like arrays

Pointers give you the address of an object in memory. It can be a "standalone object" like in the example above, or it can be an object that is part of an array. Neither the pointer type nor the pointer value can tell you which one it is. Just the programmer. That's why

Pointers also have the array subscript operator defined:

p[i]

evaluates to the ith element to the "right" of the object pointed by p. This assumes that the object pointer by p is part of an array (except in p[0] where it doesn't need to be part of an array).

Note that p[0] is equivalent to (the exact same as) *p.

Arrays, acting like pointers

In most contexts array names decay to a pointer to the first element in the array. That is why many beginners think that arrays and pointers are the same thing. In fact they are not. They are different types.

Back to your question

what is meant by &arr

As said above, arr is not a pointer. So &arr means pointer to array. This is different from pointer to the first element of the array (&arr[0]).

SO what is the difference between a pointer to an array and a reference to an array?

Well, first of all please read What are the differences between a pointer variable and a reference variable in C ?. Second of all... well that's pretty much it.


the bulk of this answer is copied from this other answer of mine

CodePudding user response:

There are 3 different concepts that you need to think about:

  • Pointer : the pointer is a variable that is used for storing the address of another variable.
  • Array: An array is a collection of variables of a similar data type
  • Reference : A reference variable is an alias, that is, another name for an already existing variable.

Here is a summary of how you can connect these three variables together:

arr == &arr[0]

and

*arr == arr[0] 
  •  Tags:  
  • c 11
  • Related