Peter Olson
Peter Olson

Reputation: 143037

This doesn't look like a function. What is this?

A friend asked me to write a function in C to return the 100th element of an array. I'm not very familiar with C, so I wasn't sure how to make a generic function that could do this with any type of array, so I cheated and assumed that it was an array of integers and wrote this function:

int GetHundredthElement(int *array) {
  return array[100 - 1];
}

(the - 1 is there because arrays are zero-indexed)

I asked him how to make a function that would work for any type of array. He told me there was a simple way to do it:

int GetHundredthElement = 100 - 1;

and that this "function" could be called like this:

GetHundredthElement[array];

I tried it, and it worked, but doesn't look like a function to me because it uses bracket notation, which isn't how function calls are written in C. I don't really understand exactly what this code is doing or how it's doing it. What's going on here?

Upvotes: 4

Views: 287

Answers (5)

Corey Ogburn
Corey Ogburn

Reputation: 24769

In C, the bracket notation is short hand for pointer arithmetic. A normal use like x[i] is syntactically equivalent to *(x + i) (remember that arrays are pointers). If instead you had used i[x] then it's the same as *(i + x) and produces the same output. As noted by other answers, this approach is not a function, just an interesting technique.

Upvotes: 2

wkl
wkl

Reputation: 80041

This is the special funky way you can access arrays.

Let's recall that arrays can be treated like pointers, and you can use arithmetic.

For example:

let x be some arbitrary array:

int x[4];

Implies accessing x's 5th element, which is *(x+4).

Terrible example of memory layout:

x -> [0][1][2][3][4]

Now, the weird thing you can do with C arrays/pointers to blocks, is flip the number and variable in bracket notation.

x[4] is equal to 4[x]

Because they break down to:

*(x+4) and *(4+x)

Upvotes: 2

Platinum Azure
Platinum Azure

Reputation: 46233

You are right about the fact that GetHundredthElement is not a function-- it is, as you would expect, an integer.

However, this illustrates a surprising ability in C where you can reverse the order of your array access!

assert(a[5] == 5[a]);

This is because an array access can be implemented in pointer arithmetic:

assert(a[5] == *(a+5));
assert(*(a+5) == *(5+a));
assert(*(5+a) == 5[a]);

Upvotes: 9

Mahmoud Al-Qudsi
Mahmoud Al-Qudsi

Reputation: 29579

It's not a function, it simply takes advantage of a little-known C fact that array indexes can be interchanged. All the x[y] notation really means is that you're accessing the xth offset of the y array. But you could just as easily write y[x] in your case and get the same result.

99[array] and array[99] are interchangeable and mean the same thing. By declaring GetHundredthElement to be 99, your friend played a neat trick :)

You CAN however write a generic function to get the hundredth element of an array fairly easily using C++ templates (not C).

Upvotes: 11

K-ballo
K-ballo

Reputation: 81409

When using pointers, pointer[ index ] and index[ pointer ] are actually the same. It's not a function, its a regular operator; its the same as array[ GetHundredthElement ] or array[ 100 - 1 ].

Upvotes: 5

Related Questions