Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
C++
int *(crack)();         // defining a function which return a pointer integer
int (*crack());         // defining a function which return a pointer integer
int *(*crack2)();       // defining a variable that will hold the address of a function. the function return type is pointer integer
int (*crack3)();        // defining a variable that will hold the address of a function. the function return type is integer
int ((*(crack5))());    // defining a variable that will hold the address of a function. the function return type is integer
int *(crack4());        // defining a function which return a pointer integer
int (*(*crack6)());     // defining a variable that will hold the address of a function. the function return type is pointer integer
int ((*(crack8)()));    // defining a variable that will hold the address of a function. the function return type is integer
int *(*((crack9)()));   // defining a function which return a pointer of pointer integer i.e. int **crack9()
int (*((*(cast()))));
int (*whatever(int justaval))(int param1, int param2); //define pointer of function which return pointer of function // i am exhausted 



I am having a really hard time to differ between this things. I know which one is what but life is getting hell while I am trying to make a general rule to differ between variable, function and pointer variable of functions.

So, my question is where can I find a general definition or set of rules that would guide me to write a general rule to differ between these
Posted
Updated 22-Jul-13 16:13pm
v2
Comments
pasztorpisti 23-Jul-13 7:20am    
The difference between the function declaration and the function pointer declaration is the '*' in front of the function name and the '(' and ')' around the '*'+funcname. The difference between declaration and definition: In case of definition you give the function definition/body after the header in between '{' and '}' while you end a function or function pointer declaration with ';'. Something that is more complicated is a C++ method pointer declaration. There the only difference is that instead of a '*' you prefix the method name with 'ClassName::*'.

My advice: Don't write unreadable/write-only code. C/C++ allows you to do that but you shouldn't. Maybe in C you will need to declare a pointer to a function that returns a pointer to another function (once in 5 years) but even in that case I would do that this way:
C++
typedef int (*TReturnedFuncPtr)(int param1, int param2);
typedef TReturnedFuncPtr (*TOtherFuncPtr)(void* param);
TReturnedFuncPtr Implementation(void* param)
{
    ...
}

In good quality C++ code you will never see function pointers, only in case of cooperation with C code (including the operating system APIs) but even in those cases usually only a single function pointer is involved. If you see code like the last line of your example in a C program then its pretty sure that it has been handcrafted by a beginner or a magician. If you see the same in C++ code that its guilt and lack of understanding of OOP.

If you are forced to read such ugly declarations: nv3 has already pointed it out: Reading function pointer declarations is easiest by starting out from in the middle from the name of the function pointer type. In my opinion both allowing such ugly declarations (for example allowing more func ptr declarations inside one declaration) and keeping the name of the type in the middle of the declaration were bad decisions in the C language.

EDIT: Don't waste your time on deciphering such declarations. Usually neither bad nor good quality code contains declarations like that. Its fine to decipher one specific declaration once in 5 years if you find some ugly piece of code. Use your time to gain true knowledge and waste your time on learning these things only if you are forced to. Its only a good thing that you did a lot of C/C++ programming without encountering such things.
 
Share this answer
 
v3
Comments
nv3 23-Jul-13 7:20am    
Good points! And yes, declaring functions pointers is so much easier when using a typedef for the function signature and declaring the pointer with its help.
Mohibur Rashid 7-Aug-13 22:14pm    
I have accepted this as answer, cause I am giving a thought of not allowing any callback or function pointer, cause so far I don't find a reason to have a callback. So far, I have never faced a situation where I would have to transfer a pointer of function except those time when I was really bored and has nothing better to do, even when I was developing an application. Its impossible to say whether all apps can be developed without callback function. But for web may be i can skip this part.
pasztorpisti 23-Jul-13 7:24am    
Thank you!
Mohibur Rashid 23-Jul-13 9:33am    
thanks for your comments,
I am writing an application whose job is to translate some code to equivalent c++ code. I want to keep the C++ declaration style. That is why I need to analyze this.
pasztorpisti 23-Jul-13 9:49am    
You are welcome! What do you mean on "C++ declaration style"?
I agree that on first sight these declarations look confusing. But once you know the idea behind them, things become easier. The main idea of C and C++ declarations was:

"Declare things the same way they are used"

So always start at the identifier and think of the entire construct as a regular C++ expression. Then resolve the expression by using the C++ precedence and associativity rules. Example:
C++
int ((*(crack5))()); 

Start at crack5. Peel off the first set of parentheses, as they do nothing. Then we have left:
C++
int ((*crack5)()); 

The only thing we can do now is dereference the pointer operation. Hence crack5 is some kind of a pointer.
Then in the next step, the only thing we can do is perform a function call. Hence crack5 must be a pointer to a function.
Peel off one redundant set of parentheses again and we see that the function returns an int.

It takes some time to get used to this kind of reading a declaration. I would recommend you take a deeeeep look at the C++ precedence rules, as they are the key to all this.
 
Share this answer
 
Comments
Mohibur Rashid 23-Jul-13 5:37am    
thanks, this might help too
I think you are talking about understanding c style declarations. There is tons on the net about this. For example:

http://www.unixwiz.net/techtips/reading-cdecl.html[^]

and

How to interpret complex C/C++ declarations[^]
 
Share this answer
 
Comments
Mohibur Rashid 23-Jul-13 0:38am    
these might help
I think you just need to identify the scope of *( int * or *func() ).
 
Share this answer
 
Comments
Mohibur Rashid 23-Jul-13 0:36am    
I don't agree with your thinking, I have developed tons of application with c/c++, so my question is beyond that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900