-1

today i started learning C, and i got stucked in function pointers. This is my code:

main.c:


#include <stdio.h>

int sumOfElements(int *arr, int arr_elements);

int main()
{

    int (*ptr)(int,int) = NULL;
    ptr = sumOfElements;
    int a[] = {128, 64, 32, 16, 8, 4, 2, 1};
    printf("Total of price is: %d", ptr(a, 8));

}

int sumOfElements(int *arr, int arr_elements)
{
    int k =0;
    int total;
    for(;k < arr_elements;k++)
    {
        total += arr[k];
    }
    return total;
}

What i'm trying to do is access the elements of the array in the sumOfElements functions. When i call it normally, then everything goes smooth. When i try to use the function pointer, then the compiler throw me some warning before, and then the Segmentation Fault error:

main.c:17:9: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]                               
main.c:19:41: warning: passing argument 1 of ‘ptr’ makes integer from pointer without a cast [-Wint-conversion]              
main.c:19:41: note: expected ‘int’ but argument is of type ‘int *’    
Segmentation fault (core dumped) 

Since i'm still learning it, i'm unsure about touching the code, because, like i said before, it works without the function pointer. Now, the error main.c:17:9: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types], i didn't really understand it, because they're both int. So, if you could explain that too, it would save me a lot of time. So, why does it only throw Segmentation Fault (core dumped) when i use the funptr? All i know is that the Segmentation error is when we try to access memory that is only read-only or it's out-of-bound

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Speed
  • 5
  • 2

4 Answers4

4

For starters the pointer is declared like

int (*ptr)(int,int) = NULL;

that is it is a pointer to a function with two parameters of the type int.

But the function sumOfElements have different types of parameters

int sumOfElements(int *arr, int arr_elements);

That is the first parameter has the type int * instead of int.

Also as the array is not changed within the function then it is better to declare the function like

long long int sumOfElements( const int *arr, size_t arr_elements);

The function return type is long long int instead of int because it decreases the risk of overflow of the sum of elements of the array.

Correspondingly the pointer shall be declared like

long long int (*ptr)( const int *, size_t ) = NULL;

and the function should be called like

printf("Total of price is: %lld", ptr(a, sizeof( a ) / sizeof( *a ) ) );

Within the function you forgot to initialize the variable total.

int total;

The function can be defined the following way

long long int sumOfElements( const int *arr, size_t arr_elements )
{
    long long int total = 0;

    while( arr_elements-- )
    {
        total += arr[arr_elements];
    }

    return total;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Your function pointer declaration is of incorrect type.

Your pointer has signature of int (*ptr)(int,int) = NULL; while your function is int ()(int *, int).

Try declaring your pointer as int (*ptr)(int *,int) = NULL;

Kon
  • 4,023
  • 4
  • 24
  • 38
1

The function pointer needs to have the same parameter and return types as the function itself. In your ptr declaration you declare the first argument as int, but it should be int*.

    int (*ptr)(int*,int) = NULL;
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You have to use int (*ptr)(int*,int) = NULL; instead of int (*ptr)(int,int) = NULL;, since your array is an int pointer. As your regular function sumOfElements already receives an int pointer it works correctly.

Alberto Pires
  • 319
  • 1
  • 5
  • 10