Click here to Skip to main content
15,038,345 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::*'.

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[^]
   
Comments
Mohibur Rashid 23-Jul-13 0:38am
   
these might help
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.
   
Comments
Mohibur Rashid 23-Jul-13 5:37am
   
thanks, this might help too
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.
   
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"?
Mohibur Rashid 23-Jul-13 9:56am
   
by this I mean, when programmer will define a function or a pointer of function it would have to be c++ compatible
pasztorpisti 23-Jul-13 10:17am
   
The source I presented in my answer is C/C++ compatible and more readable than a lot of function pointers merged together in one declaration. typedef is your friend.
Mohibur Rashid 23-Jul-13 20:56pm
   
I have understood what you wanted to say, but what I wanted is not to put any barrier. I want to write the code which can read any stupid definition a programmer does as long as it is valid. That is why I am looking for a standard definition that can cover everything :)
pasztorpisti 24-Jul-13 6:47am
   
Then I misunderstood you. I thought you are writing something that outputs C++ code. From this comment I assume you are writing a C++ parser. In this case prepare for the worst. Learn the dragon book before it or use an existing parser/frontend like clang. You won't get the necessary knowledge from codeproject answers/comments.
Mohibur Rashid 24-Jul-13 8:49am
   
alright, but i got some valuable direction :)
I think you just need to identify the scope of *( int * or *func() ).
   
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