This is a ridiculously simple function that I just can not seem to get to work. I am getting a segmentation fault error for this every time I run it.
// LargestUsingPointers: receives an n-element integer array and returns
// a pointer to the largest element; the function uses pointers
int *LargestUsingPointers(const int *array, int n)
{
const int *p1;
int *largestIndex;
for(p1 = &array[0]; p1 < &array[n]; p1++)
{
if(*p1 > array[n])
{
*largestIndex = *p1;
}
}
return largestIndex;
}
I did some debugging and the program will run all the way through until it hits ->
*largestIndex = *p1;
which is where I am given the error. Also, *p1 holds the correct value at that point. So, I am lost! :) Any advice?