简体   繁体   中英

Error while running program “Segmentation fault core dumped”

I'm trying to study pointers and functions, so I decided to write a simple code. But I have a problem with variable of the new type SIGNAL. Every time when code is running, I have that mistake core dumped. I tried to rewrite code, but is still the same mistake. I think that problem with data types, which I try to send to the signalD. But I can't find out where I'm wrong.

I tried to send another data types and played with functions, but still the same result.

#include <stdio.h>

typedef void (*FUNC) (void*);
typedef FUNC (*SIGNAL) (int p, FUNC i);

void task1()
{
    printf("Run task 1\n");
}
void task2()
{
    printf("Run task 2\n");
}


 void mainProcess(SIGNAL signalD, int i, void *p,void (*f)(void*))
{
    task1();
    signalD(p,f)(i);//I'm afraid that here is a mistake with data  maybe (p,f)(f)
    printf("Run main process\n");
}

void task3()
{
    printf("Run task 3\n");
}

int main()
{
   mainProcess(task2,5,0,task3);
   return 0;
}

Program crashes when it goes to execute the signalD part. I expect that it should print: Run task 1 Run task 2 Run main process

Your task2 function needs to return a FUNC doesn't it?

FUNC task2(int i, FUNC p)
{
    printf("Run task 2\n");
    return p;    
}

Also I think you want to call it like this;

signalD(i,f)(p);

You're compiler is giving you warnings for a reason, correct them and you can arrive at some fixes for your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM