Jump to content

Function pointer problem

- - - - -

  • Please log in to reply
4 replies to this topic

#1
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
I'm trying to declare and use a function pointer in a C program. I've done the function pointer declaration, which acts as the function prototype if I'm understanding what I've read correctly, and I've also done the actual definition of the function.


#include <stdio.h>


void (*handler)( int );


int main( int argc, char **argv ){

	/*

	char c;

	signal( SIGSTOP, handler(0) );

	for(;;){

		c = getchar();

		putchar( c );

	}

	*/

	return 0;

}


void handler( int num ){

	printf( "Process stopped\n" );

}


Here's the problem: When I run the code (keep in mind the signal part is commented out; I'll get to that later) the compiler says:

sig.c:17: error: 'handler' redeclared as different kind of symbol

sig.c:3: error: previous declaration of 'handler' was here


So evidently, both the prototype and the function defintion count as declarations of the pointer. I can't have only one of them, obviously, but if I have both of them, it's declared twice.

The second problem is that signal() takes a function with a void return type as its input, but when I compile it (with comments removed) it says it can't use a void output as a parameter (makes sense, but why would it require a void-typed function in the first place?)

I've already tried defining the function as

void (*handler)( int num ){

and it didn't work.

So my three questions are:
1. What is the syntax for a prototype of a function declared through its pointer?
2. What is the syntax for the function definition of a function declared through its pointer?
3. Which is the actual declaration of the function: the prototype or the definition?
Programming is a journey, not a destination.

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
The following worked for me:
#include <stdio.h> 


void fxn(int); 

int *pFxn= (int *)(&fxn); 


int main(int argc, char * argv[]){ 

	asm { 

		// Not sure of what the calling convention is. 

		mov ebx, esp 

		push dword ptr 4 

		call dword ptr [pFxn] 

		mov esp, ebx 

	} 

} 

void fxn(int a){ 

	printf("The number is %d\r\n", a); 

} 


#3
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
Okay, that's completely different code though, and when I tried to pass a pointer to the handler to signal() it said it was undeclared. I appreciate the help, but that didn't really answer any of my three questions.
Programming is a journey, not a destination.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Okay, you don't quite understand function pointers. A function pointer does not declare anything close to a function prototype, however a function prototype does allow you to get the address of that function, which you can assign to a function pointer.
#include <stdio.h>

void handler( int );

void (*pHandler) ( int ) = &handler;

int main( void )
{
  return 0;
}

void handler( int num )
{
  puts( "Handler Executed." );
}
This will build correctly. Also, why are you trying to catch SIGSTOP, you can't catch SIGSTOP nor SIGKILL, it's a fruitless endeavor. You can see the signal mechanism working in this implementation using SIGALRM.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>


void handler( int );


void (*pHandler) ( int ) = &handler;


int main( void )
{
  signal( SIGALRM, pHandler );
  alarm(5);
  while (1) putchar(getchar());
  return 0;
}


void handler( int num )
{
  puts( "Handler Executed!" );
  exit(0);
}
A function pointer does not declare or even imply a function, simply that a set of data that represents an address in memory to a function with the type signature provided in the type declaration exists. You must provide a separate function prototype to imply the existence of a similarly named function in the future, and that the function will have an address to link to at compile time.
Wow I changed my sig!

#5
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
Okay, I've rewritten the program using the code you provided, and it seems to work now. Thanks for the help.
Programming is a journey, not a destination.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users