Skip to main content
gamedev.net
🔒 Locked

Pointer to a function problem

Started by Dr BOO 3 replies 400+ views
Original Post
Dr BOO
Dr BOO
Hi there I'm trying to get the code listed below to compile without warnings (I'm using VC++ 7.0. On compilation it gives the following warning: warning C4047: '=' : 'int (__cdecl *(__cdecl *)())()' differs in levels of indirection from 'int (__cdecl *)()' #include int (* (* cc912)())(); /*C construct here*/ int (*a)(); int c(int ca){ return ca; } int (*b)(); int main(){ a = &c b=a; cc912 = **b; return 0; } Any helped given would be most appreciated.
nmi
nmi
You mean something like this ?
#include<stdio.h>typedef int (* func_t)();//int (* (* cc912)())(); /*C construct here*/func_t* cc912;//int (*a)();func_t a;int c(){return 3;}//int (*b)();func_t b;int main(){a = &cb=a;cc912 = &breturn 0;}


Note that the assignment "a = &c" will not compile with a C++ compiler if c expects a parameter while a doesnt.

The assignment "cc912 = **b;" will not work, because b is a pointer to a function (a single level of indirection). Then "*b" will dereference that pointer (no indirection anymore). So you cannot dereference again. But since cc912 is a pointer to a pointer to a function (at least I thought that was the thing you wanted to create), it can store the address of b.
Dr BOO
Dr BOO
Thanks NMI, I apologise I should have provided further insight into the problem, the approach you suggested would work and its an approach we've adopted previously during the development of our product (mainly in getting around problems in testing)....

Basically we're developing a tool that auto generates C constructs, one of the constructs that was generated was:
int (*(*cc912)())();
This same tool now creates a set functions based on the object that was used to generate the C construct.

These functions are initialised and tested against ANSI-C compliant C compilers (C89 standard). The original code fragment I posted might have been incorrectly generated...but because I'm not too sure how to go from the auto generated C construct without hardcoding rules with typedefs which makes the code generator a bit in flexible and not generic enough especially if we increase the depth of complexity of the code generation process. That's why I hoping there might be a solution to original problem using the original code, maybe something that we've overlooked, another step maybe.
Thanks for you help though.
Enigma
Enigma
int (* (*)())() is a pointer to a function which takes no parameters and returns a pointer to a function which takes no parameters and returns an int, i.e.:
#include<stdio.h>int (* (* cc912)())(); /*C construct here*/int (*a)();int c(int ca){	return ca;}int (* b())() // typedef int (*)() func_t; func_t b(){ return a; }{	return a;}int main(){	a = &c	cc912 = &b	return 0;}

Σnigma
Dr BOO
Dr BOO
Thanks Enigma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.