1

I'm analyzing a C program in which I find a strange fucntion call here is the function definition :

static void endSignal (int32_t dummy)
{
  if (nTerminating) return;
  nTerminating=1;
  printf("terminating....\n");
  terminateDLNAsystem();
  sleep(1);
  exit (0);
}

This function takes an int32_t parameter ! Now this the main function calling "endSignal"

int32_t main (int32_t argc, char **argv)
{
/*Statements
.
.
*/
signal(SIGINT, endSignal);
signal(SIGABRT, endSignal);
signal(SIGQUIT, endSignal);
signal(SIGTERM, endSignal);

return 0;
}

the main function call endSignal without any parameter, what happen in this case ?

stojo304
  • 31
  • 11

1 Answers1

6

Main function calls signal functions and not endSignal.

endSignal is parameter acting as callback.

This is passing function pointers as arguments.

How do you pass a function as a parameter in C?

Community
  • 1
  • 1
tilz0R
  • 6,696
  • 2
  • 19
  • 35
  • This is not the same case. in my case "signal" is a pre-defined function in so signal can't call the function endSignal to use it .. – stojo304 Feb 17 '17 at 13:21
  • @stojo304 This is exactly the right answer, and of course `signal()` can call its callback function, that's the whole point. Why shouldn't it be able to? – unwind Feb 17 '17 at 13:22
  • Yes i just read the definition of signal and understand this point, Thanks – stojo304 Feb 17 '17 at 13:25
  • @unwind , please what is the difference between : int *i[]= {1,1}; int j[]={2,2}; – stojo304 Feb 17 '17 at 13:45
  • first is array of pointers, both points to value 1. In second case, you have 2 integer values, both are set to 1. – tilz0R Feb 17 '17 at 13:46
  • so i[0] is a pointer not an integer ? because when i print : i[0]=1 ! – stojo304 Feb 17 '17 at 13:49
  • Variable itself has a value. "Pointer" exists only for compiler to interpret variables. If you print pointer, it will print as normal variable. If you want to print content on memory where pointer points, use printf("....someModified", *i[0]); Please, don't be agressive. – tilz0R Feb 17 '17 at 13:52
  • @stojo304 Your comments have nothing to do with the question, that's strange. This is not a discussion forum, please post a new question if you think it makes sense to do so. – unwind Feb 17 '17 at 13:52