2

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

3
  • 2
    Are you saying that the compiler crashed( I doubt that)? Or, you are getting this error when you RUN your program? Commented Dec 6, 2013 at 17:17
  • error at run time, it's solved the error was the use of swapp in the sub function it's solved now thx guys :) Commented Dec 6, 2013 at 17:25
  • Turn on warnings in your compiler ... that would have caught this. And learn how to use your debugger, don't ask SO for help debugging individual programs. Commented Dec 6, 2013 at 17:54

1 Answer 1

6

need prototype

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

and

swapp(a,b);

to

swapp(&a,&b);
Sign up to request clarification or add additional context in comments.

2 Comments

If this answer helped you then you would like to accept it. Read about page for more detail.
@haccks I appreciate your kindness

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.