pointers to function error

hello guys i'm kinda new in here and in c also i've been practising pointers to functions and i've come up with this code :

 #include <stdio.h>
#include <stdlib.h>
typedef int (*fptr)(int,int);
void swapp(int*,int*);
int sub(int ,int );
int add(int ,int );
int operation(fptr,int,int);
int main()
{
    int n=10,n1=25;
    printf("%d+%d=%d",n,n1,compute(add,n,n1));
    printf("\n%d-%d=%d",(n>n1)?n:n1,(n<n1)?n:n1,compute(sub,n,n1));


    return 0;
}
void swapp(int *a,int *b){
    int temp=*a;
    *a=*b;
    *b=temp;
}
int add(int a,int b){
    return a+b;
}
int sub(int a,int b){
    if(a<b)
    swapp(a,b);
    return a-b;
}
int compute(fptr operation ,int a,int b){

    return operation(a,b);
}

when compiled i get "Segmentation Fault(core duped) can anyone help me debug this? and thx i just can't see anything wrong with the code

Solutions

need prototype

int compute(fptr operation ,int a,int b);

and

swapp(a,b);

to

swapp(&a,&b);
Tags: C / Function / Pointers

Similar questions

Memory allocation error for structs with members that are function pointers and void pointers
I have written a straightforward C code that uses an engine to run two different algorithms depending on user input. It uses function pointers to the algorithm methods and objects. There is a nasty memory bug somewhere that I can not track down, so maybe I am allocating memory in the wrong way. What is going wrong? Below is (the relevant parts of) ...
Passing arrays and matrices to functions as pointers and pointers to pointers in C
Given the following code: I don't understand why I get this warning: warning: passing argument 1 of ‘bar’ from incompatible pointer type Although 'foo' call seems to be ok. Thanks :)
local pointers, `static` pointers and `malloc` pointers
In foo, I assume: c1 is a local pointer, so it should be on the stack; c2 is a static pointer, it should be on the heap; c3 is on the heap. According to my assumption, I draw a graph about the pointers and the string literal they're pointing, My assumption and graph right? Still, I don't quite understand why should c3 be on the heap? c3 is just a c...
Pointers within pointers within pointers
There are many questions on pointers when it comes to structs here, but what I am doing here is a little bit more complicated and has left me stumped. I have a proc struct for xv6. I am attempting to implement mmap which takes a fd and a 0 for 'eager_mmap' or 1 for 'lazy_mmap'. Inside of this proc struct, I declare an array of structs which will co...
Reallocating pointers of pointers of pointers
As a part of our Computer Science course (using C), we are to build a VERY wasteful system using pointers Since we are not allowed to use structures at this point, we are to use ONLY pointers for our dynamic-arrays. I have created the dynamic-array **students and allocated space for it. at this point, i send this dynamic array (**students) to a fun...
C
Trouble with pointers and pointers to pointers
I'm having some trouble understanding why a piece of code works. The following is a comparison function for stdlib's implementation of qsort: Naturally, this only works for strings. My question is: why does the code below work? It seems to me that in the second version, I'm forcefully casting what clearly is a char* to char**. The problem is that t...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.