1

Why isn't my code working?

#include <stdio.h>

int main()
{
    int test[] = { 3, 9, 7 };
    printf("%d", find(test, 3));
    return 0;
}

int find (int array[], int size)
{
    int i, largest, where;
    largest = array [0];
    for (i=0; i<size; ++i) {
        if (array[i]>largest) {
            largest = array[i];
            where = i;
        }
    }
    return (int) *(&array+sizeof(int)*where);
}

I know I can replace:

return (int) *(&array+sizeof(int)*where);

with:

return array[where];

but that isn't the point of the exercise.

3 Answers3

3

Pointer arithmetic doesn't work the you think it does. You're looking for:

return *(array + where);

array is already a pointer, and pointer arithmetic just "does the right thing" when adding.

You might be confusing yourself because of the int array[] in your function signature - that's just syntatic sugar for int *array. You really have a pointer, not an array.

Since there is some misinformation flying around in other answers, I'm going to write a more complete explanation here. This function signature:

int find(int array[], int size)

really means:

int find(int *array, int size)

The use of the [] is just syntactic sugar, as mentioned above. When you call the function, like:

find(test, 3);

test is decaying automatically into a pointer to its first element. It's identical to if you had called:

find(&test[0], 3);

Now, looking at your return statement, you can see that:

return (int) *(&array+sizeof(int)*where);

Doesn't make sense - first of all &array is the address of parameter - in this case a pointer parameter, but that doesn't really matter. If you add to it and dereference, you're going to return some random data from your stack, not not from the array you want to.

Secondly, the multiplication by sizeof(int) is unnecessary. Pointer arithmetic in C already includes an implicit multiplication by the size of the pointed-to type. What you're really trying to return is:

 return array[where];

which is equivalent to:

 return *(array + where);
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • So array is basically &array[0]? Why do you just add where? Shouldn't you add sizeof(int)*where? – user2258753 Apr 08 '13 at 18:05
  • @user2258753, `sizeof(int)` is unncessary - pointer arithmetic already includes an implicit multiplication by the size of the pointed-to type. `array` is *NOT* `&array[0]` in this case, since `array` is a pointer, not an array. If it *were* an array, then yes - in most situations the name of the array would decay into a pointer to its first element. – Carl Norum Apr 08 '13 at 18:07
  • @user2258753, actually, he didn't. His answer contains inaccuracies based on your use of `array` in your function. – Carl Norum Apr 08 '13 at 18:08
  • Is there a distinction made between a pointer to the first element of an array and an array by itself? – user2258753 Apr 08 '13 at 18:11
  • @user2258753 - yes, one is an array and the other is a pointer to the first element of the array. You should probably check out [the relevant section of the `comp.lang.c` FAQ](http://c-faq.com/aryptr/index.html). – Carl Norum Apr 08 '13 at 18:13
2

You're greatly overcomplicating this...

return array[where];  // this means, take the base memory location of "array" then
                      // add in an offset of where and dereference the result

That's exactly the same as:

return *(array + where);

The fact is that in your main function you have an array called test, once you pass this to your find() function the array will decay to a pointer. So if you now know that you have a pointer you should see why you shoud not be grabbing the address like this:

&array

And you don't need to be adding the "sizeof" offset, because that's given by the type of the pointer (int *)

Community
  • 1
  • 1
Mike
  • 47,263
  • 29
  • 113
  • 177
  • I think the OP is confused because he thinks he has an array, when all he has is a pointer to the array's first element. – Carl Norum Apr 08 '13 at 18:04
  • Re: your edit: `&array` is actually *completely wrong* in this case, since `array` is a pointer parameter to the function, *not* an array. – Carl Norum Apr 08 '13 at 18:06
  • @Carl - Yeah, I was just trying to think of a better way to explain *why* it was wrong, but yes, saying "unneeded" is incorrect. – Mike Apr 08 '13 at 18:13
  • Your new edit is better. I just thought 'unneeded' sounded like 'unnecessary, but ok', which it wasn't in this case. +1. – Carl Norum Apr 08 '13 at 18:14
0

You should do:

return  *(array+where);

since where is the offset to the array. You don't need sizeof(int) anymore

taocp
  • 23,276
  • 10
  • 49
  • 62