-1

My program need to have a function in which it finds the largest element of an array. It then returns the position of largest element. However, when finding the largest element, I am only allowed to use pointer arithmetic.

#include <stdio.h>

int *largest(int *array, int size){
    int *p1 = array;
    int *count, max = *p1;
    for(*count = 0; count < p1 + size - 1; p1++){
        if (max < *p1){
            max = *p1;
            count++;
        }
    }
    return count;
}

int main(void){
    int *array, size = 10;

    printf("enter elements; ");
    for(int i = 0; i < size; i++){
        scanf("%d ", &array);
        array++;
    }

    printf("\nThe largest element in the array is in element %d", *largest(array, size));
}

However, when I run the program, after enter values for the elements, it gives me:

segmentation fault (core dumped)
Shoaib Ahmed
  • 115
  • 2
  • 5
  • 1
    Try to allocate some memory for the array? Then assign integers to each array position? Then don't increment array because then it doesn't point to the base anymore? (*for starters*) – Déjà vu Mar 08 '17 at 07:00
  • 1
    You didn't allocate memory for the array. You need to add line `array = (int*)malloc(sizeof(int)*size);` – Jirka Picek Mar 08 '17 at 07:01
  • 1
    You didn't initialize `count` before dereferencing it. – Bob__ Mar 08 '17 at 07:10
  • 1
    You're also reading an integer into an `int*` which is unlikely to end well. – Raymond Chen Mar 08 '17 at 07:45

1 Answers1

1

Mistakes in the function main:

You didn't allocate memory for the array. You need to add line array = (int*) malloc(sizeof(int) * size);. Without this line you get segmentation fault.

Another mistake is in filling array. Array is pointer already so you don't have to reference it again. Now you are changing pointer, not array values

Potential bug: In loop when you fill array, you increase pointer, so you loose information about beginning of array. You can get it again after loop doing array = array - size;, but it won't work if you break loop earlier. It is better to work with temporary pointer.

Mistakes in the function largest:

You created pointer count, which is not allocated and then in for loop initialization you write value 0 to unknown address. Causes segmentation fault.

In for loop you are trying to read memory from address 0 to address array + size - 2, but you want to read it from beginning of array to the end of array which is from array to array + size - 1.

Returning count makes no sense, but to sum it up, look at the example.

Pointer is just number which is address to memory, so when you don't allocate it or don't assign existing pointer, then it points randomly to memory and OS don't let you access that memory. It can happen that you will read something from random pointer, but new compilers sets initial value to 0. In BSD you can read from address 0, but linux cause segmentation fault (I hope that I didn't switch it). Writing to address 0 causes segmentation fault in both systems.

You should also check in function if you got valid pointer and allocation can fail so you need to check pointer immediatelly after allocation too. To sum this: Never trust pointer, always check it's validity.

Example doesn't check validity of pointers.

#include <stdio.h>

int *largest(int *array, int size){
    int *end = array + size; // address behind array
    int *max = *array; // address with largest value
    for(; array < end; ++array){ // you dont need initialization since the array points to beginning
        if (*max < *array){ // compare values, not address
            max = array; // save position of pointer with largest value
        }
    }
    return max; // return address with largest value
}

int main(void){
    int *array, size = 10;
    array = (int*)malloc(sizeof(int) * size); // allocate memory

    printf("enter elements; ");

    int *tmp = array; // temporary variable to not loose information about beginning of array
    for(int i = 0; i < size; i++){
        scanf("%d ", tmp); // reference is not needed since tmp is already pointer to value
        tmp++;
    }

    printf("\nThe largest element in the array is in element %d", *largest(array, size));
}
Jirka Picek
  • 589
  • 5
  • 19