user2258753
user2258753

Reputation: 25

How to navigate through arrays using pointers?

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.

Upvotes: 1

Views: 2397

Answers (3)

taocp
taocp

Reputation: 23644

You should do:

return  *(array+where);

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

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 224962

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);

Upvotes: 3

Mike
Mike

Reputation: 49403

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 *)

Upvotes: 2

Related Questions