Click to See Complete Forum and Search --> : pointers
stingertto
February 19th, 2002, 02:57 AM
this might seem trivial to some of u but i'm jus unsure:
i need to use a pointer to a function
void suspend_resume()
i wanted to use a long pointer but the compiler only lets me define the pointer as
void (*lp)()=suspend_resume;
my question is wat data type would this pointer be(long,int)?
and how do i make the pointer global so i could use it in another function in my project?
Graham
February 19th, 2002, 03:33 AM
Its type is void (*)(). In C++, that means "pointer to a function taking no arguments and returning void". In C it means "pointer to a function with no argument information and returning void".
As for making global: the same as any other - for preference, don't. If you must, define it in a .cpp/.c file and declare it extern in a .h file.
He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
NMTop40
February 19th, 2002, 04:35 AM
The type is void (*)() but it is clearer sometimes to use typedef, thus:
typedef (*pfnVoid_t)();
Then you can call this type pfnVoid_t whenever you want to use it.
pfnVoid_t myFunction = suspend_resume;
You can also typedef it without the * as follows:
typedef void (fnVoid_t)();
then you need to use a pointer when you want to assign to a real function: thus
fnVoid_t *myFunction = suspend_resume;
This means less ugly code but it also means another programmer has to go off and look up somewhere what your typedef means, whereas at least with the "ugly" notation one can see immediately what it means.
I prefer to use these typedefs only scoped, either in a class or a namespace.
As for making your function pointer global, it might be better instead to externally define a function that returns your function pointer. This will allow it to be externally accessed but not modified.
Thus:
// in .h file
extern pfnVoid GetFunction();
// in C++ file that owns the function
pfnVoid GetFunction() { return myFnPtr; }
// in other C++ file
GetFunction()(); // second set of brackets to execute the function
(Of course use more meaningful descriptor than GetFunction() )
The best things come to those who rate
stingertto
February 19th, 2002, 07:23 AM
thanx
but a more meaningful descriptor?explain pls
NMTop40
February 19th, 2002, 07:45 AM
I mean that the name of your get function should give some description as to what the function it is returning does.
Someone who sees a line of code that looks like this:
GetFunction()();
will immediately as "what does that line actually do?" Ok, they will see that it retrieves a function pointer and executes it, but what is this function pointer?
The best things come to those who rate
stingertto
February 19th, 2002, 10:00 PM
hey
when i try to use
pfnVoid_t myFunction = suspend_resume;
i keep getting errors
'suspend_resume not being declared in this scope'
and 'anci c++ forbids declaration of pfnVoid_t without a type'
any suggestions
NMTop40
February 20th, 2002, 05:22 AM
you need to tell me what you are doing where.
If suspend_resume is a class member function then the prototype won't work.
I assume you did:
typedef void (*pfnVoid_t)();
The best things come to those who rate
stingertto
February 20th, 2002, 12:32 PM
ok
i dont have any classes- at least not yeti'm more accustomed to c-programing and even my experience in that isnt vast
so basically suspend_resume() is a fn in the same source file
i'm using a timer to interrupt the running of a process and suspend_resume() is supposed to be the timer proc callback fn
thats y i need to get a pointer to this fn, it should be an argument in the SetTimer() fn
hope i'm clear
NMTop40
February 20th, 2002, 06:59 PM
This is the same problem as your other one then.
You can write Windows programs in C too - using Win32 SDK, although it's worth learning C++. A lot of the things you need function pointers for in C are replaced with virtual functions, which are much simpler.
I think you probably want a multi-threaded program, but you haven't said anywhere what your two "processes" are.
The best things come to those who rate
stingertto
February 20th, 2002, 10:36 PM
my two processes are jus simple programs i write my self
reading a file and outputting something
calculating some averages
jus a couple simple programs where each would run for more than a few seconds so i can interrupt it in a time that the user could appreciate wats goin on
i'm really stumped
my major problem here is a timing mechanism to stop the process after say bout 2-3 seconds
then i need to suspend the program so i could resume it
if i use the SetTimer() and i have the timer callback as NULL would control be passed back to the main() fn where i started the process? or is the event jus sent and the process continues to run?
i'm not sure the WaitFor...() fn would accomplish what i want
not from the reading i did anyways
if i'm wrong of course tell me!
pls
any help u could offer
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.