Function Pointer Argument

int (*ptr)(char (*ch)[]); 

What does the above expression mean? does it mean

Is ptr a pointer to a function that takes an argument, which is an array of pointers to characters that return an integer?

How to rate?

+7
source share
4 answers

Rule: http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html

In short, you should start with the identifier, and then parse everything from the identifier to the right (it can be () - a function or an array [] ), and then parse everything from the identifier to the left. Brackets change this order - you must first parse everything in the innermost brackets, etc., It works as with arithmetic calculations.

In other words, there is a priority order (which can be changed in parentheses), from higher to lower:

1) () is a function and [] is an array from left to right;

2) * - pointer, type, type modifier, from right to left.


Your example

 int (*ptr)(char (*ch)[]) 

We start with the id

 int (*ptr)(char (*ch)[]); // (1)ptr |_| 1 

The ptr identifier is in parentheses, so first we parse everything in the parent

 (*ptr) // (1)ptr |_| 1 

There is nothing to the right, so we analyze on the left

 (*ptr) // (1)ptr is (2)a pointer ||_| 2 1 

We ended up in parentheses, now we parse to the right of the parentheses

 int (*ptr)(char (*ch)[]); // (1)ptr is (2)a pointer to (3)function ||_| |____________| 2 1 3 

So far, we have ignored function arguments and parsed to the left of parentheses

 int (*ptr)(char (*ch)[]); // (1)ptr is (2)a pointer to (3)function which returns (4)int |_| ||_| |____________| 4 2 1 3 

In the same way, we analyze the function argument (I inserted some spaces for better alignment)

 char (* ch )[ ] // (1)ch is (2)a pointer to (3)array of (4)chars |___| | |_| |_| 4 2 1 3 

Finally, we have:

ptr is a pointer to a function that returns an int and takes a pointer to an array of characters as an argument

+3
source

ptr is a pointer to a function that takes an argument , which is a pointer , to an array of characters that returns an integer.

+4
source

As you already wrote, ptr is a pointer to a function that returns an int and takes a pointer to a char array as an argument.

However, you are not allowed to have an array pointer without binding to an array. Thus, your variable is incorrectly specified and will not compile. It seems you want ptr have a type that can take a pointer to a function that can take any array of sizes. This requires the construction of a template. For a function argument, it will take the following form:

 template <unsigned N> int foo (int (*ptr)(char (*)[N])) { //... } 

Usually the simplification of such types is to use typedef to represent complex parts, so that the variable itself becomes a simple pointer to some type. This is especially useful when trying to write a function that returns a pointer to a function.

 void x (char *s) {} typedef void xtype (char *); void (* y_hard ())(char *) { return x; } xtype * y_easy () { return x; } 

However, the parametric nature of the function argument complicates the task. Assuming C ++ 11, you can use the following construct ( thanks to this answer ):

 template <unsigned N> using ArrayArg = const char [N]; template <unsigned N> using Function = int (ArrayArg<N> *); template <unsigned N> int foo (Function<N> *ptr) { //... } 
+2
source

It works great at GCC .

Yes ptr is a pointer to a function. This is a pointer to a function that returns an integer and takes a pointer to an array of characters as an argument.

Consider the fun function with the following prototype,

 int fun(char (*ptr)[]); 

fun() is a function that takes a pointer to an array of characters as an argument.

and the following code fragment compiles without any errors or warnings,

 int (*ptr)(char (*ch)[]); ptr=fun; 
+1
source

All Articles