0

While I was learning C++, I came across this piece of code:

int &get(int *arry, int index) { return arry[index]; }
int main() {
    int ia[10];
    for (int i = 0; i != 10; ++i)
        get(ia, i) = i;

My question is, how does it work?

How is this possible since arry is a pointer?

Afonso Matos
  • 2,406
  • 1
  • 20
  • 30
  • 2
    Do you have [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) ? – Paul R Jul 01 '15 at 15:31
  • @PaulR I am reading C++ Primer 5th Edition – Afonso Matos Jul 01 '15 at 15:31
  • The Prata books have always been excellent IMHO. – Cloud Jul 01 '15 at 15:31
  • 1
    OK - read up on pointers and references and try and understand the differences. – Paul R Jul 01 '15 at 15:32
  • 1
    `operator[]` dereferences the pointer. Basically treats it as `*(arry+index)` – twentylemon Jul 01 '15 at 15:32
  • Relevant: http://stackoverflow.com/questions/1461432/what-is-array-decaying – Barry Jul 01 '15 at 15:34
  • @afonsomatos: As Quentin correctly stated in the answer below, operator `[]` *requires* a pointer. It exclusively works on pointers and pointers *only*. So, your "How is this possible..." question means that you need to get and read a good basic book on C or C++. – AnT stands with Russia Jul 01 '15 at 15:49
  • @afonsomatos please do not edit the body of the question if it invalidates already posted answers. The title is fine, for future searchers :) – Quentin Jul 01 '15 at 15:53

3 Answers3

3

The built-in [] syntax, in fact, works only on pointers. When you use it on an array, the array is first decayed into a pointer to its first element, which is then indexed.

In your example, the array is decayed when you pass it as argument to the function. There is no array reference here.

Community
  • 1
  • 1
Quentin
  • 62,093
  • 7
  • 131
  • 191
0

In C++, you can think of an array--in this situation--as a pointer to the first item in that array, where all of the items in the array are lined up one after the other in memory:

 _____
|  6  |   <-- ai
|  9  |
|  4  |
|__2__|

So then, *ai should yield 6.

*(ai+1) should be 9 and so on...(as it turns out, ai[x] is syntactic sugar for--it is directly converted to-- *(ai+x))

When you pass in the array, you're only passing a pointer to the first item. The function then returns the value of arry[index] by reference. This means that if we change the return value, we are actually changing arry[index], which we know to be ia[i].

Does this make sense?

Community
  • 1
  • 1
scohe001
  • 15,110
  • 2
  • 31
  • 51
0

Let the base address of an array a[10] be 1000. So, if you want to access the element in index 2 of the array a, you write a[2]. The interpretation of this statement according to the compiler is:

a[2]= 1000 + (2 * 2) = 1004

Thus you access any element by the formula :

a[index]= base_address + (sizeof datatype * index no)

Now, coming to your question, when you give only the name of the array as in ai in your case, you are actually passing the base address of the array. This is the reason you are using the pointer sign in the function parameter.int *arry, int index.

You must be knowing the following :

int a,*b,c;
a=8;
b=&a;
c=*b;

When you print c, you will get 8.

Hence, if index is 2, the line arry[index] is interpreted as :

(base_address) + (sizeof int * 2)

Richa Tibrewal
  • 468
  • 7
  • 26