Genghis Conn
Genghis Conn

Reputation: 51

helper function using pointers

I'm trying to implement a helper function that sums the ints in a given array, and then changing the "array_sum" variable in the main function using pointers NOT RETURNING AN INT IN HELPER FUNCTION. However, I get the following error, "Segmentation Fault (core dumped)" any help would me much appreciated

void get_sum(int array[], int* array_sum)
{
    int length = sizeof(array)-1;
    int i = 0;
    for(i=0;i<length;i++){      
        *array_sum+= array[i];
    }   
}
main()
{
  int array_sum;
  int my_array[] = {25, 18, 6, 47, 2, 73, 100};

  get_sum(my_array, array_sum);

  printf("sum = %d", array_sum);

}

Expected result:

sum = 271

actual result:

Segmentation fault (core dumped)

Upvotes: 1

Views: 454

Answers (3)

Govind Parmar
Govind Parmar

Reputation: 21572

You are passing array_sum by value when the function expects its address. The segmentation fault occurs because the lines in the function where you dereference array_sum will try to treat its value as a memory address.

Also, when an array is passed as an argument to a function, it decays into a pointer to its first element, losing length information. sizeof will not give you the length of the array in this case.

You may either pass a “length” parameter to the function or use a sentinel value at the end of the array to indicate to the function when to stop processing the array. The former option is the least hassle.

Upvotes: 1

K.Kaland
K.Kaland

Reputation: 61

Array decay to pointer to it's first element when passed as argument to a function, in you're case

sizeof(array) = sizeof(int*) = 8

but I think the segmentation fault is not from that line as it happens that my_array has exactly 7 elements, and the length calculated in the get_sum function is also (coincidence) 7. first of all you need to initialize array_sum, and replace the line

get_sum(my_array, array_sum);

by

get_sum(my_array, &array_sum);

Upvotes: 0

dbush
dbush

Reputation: 225737

The function get_sum expects a pointer to an int for the second parameter, but you're passing an int instead. Your compiler should have warned you about this.

As a result, the current value of array_sum (which is indeterminate because it has not been initialized) is treated as a pointer value which your function subsequently dereferences. Dereferencing an invalid pointer (as well as reading an uninitialized value) invokes undefined behavior, which in this case causes a crash.

You need to pass the address of array_sum to the function. This gives you a pointer to an int which matches what the function expects so you can modify the value in the function.

get_sum(my_array, &array_sum);

Also, unrelated to this, the following inside of get_sum is not correct:

int length = sizeof(array)-1;

The parameter array is not actually an array but a pointer to the first element of an array. So sizeof(array) gives you the size of a pointer. You might get away with it in this particular case because, assuming a pointer is 8 bytes on your system, length gets set to 7 which happens to be the number of elements of the array my_array in main. If you added or removed elements things would suddenly break.

The proper way to handle this is to get the size of the actual array in main and pass it to your function. So the function would look like this:

void get_sum(int array[], int* array_sum, int length)

And you call it like this:

int my_array[] = {25, 18, 6, 47, 2, 73, 100};
int len = sizeof(my_array)/sizeof(my_array[0]);

get_sum(my_array, &array_sum, len);

Upvotes: 1

Related Questions