Where are the parameters of this function?

void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d); 

The return type is void *, and proto_type is the name of the function? Or is it a function pointer? What settings:

(long int, char *b) or this (const char *b, unsigned short int d)

Please explain how this function works.

+4
source share
8 answers
 void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d); 

This is a feature announcement. The name of the proto_type function, which takes two type parameters:

  • long int
  • char *

and returns a pointer to a function that takes

  • const char*
  • unsigned short int

and returns void* .

Now, if you use typedef, all of the above will become apparent:

 typedef void* function_type(const char *b, unsigned short int d); function_type* proto_type(long int, char *b); 

Hope this helps.

+6
source

This declares proto_type as a function that takes (long int, char *) and returns a pointer to a function that takes (const char *, unsigned short int) and returns a pointer to void.

In other words:

 typedef void * (*R)(const char *, unsigned short int); // return type R proto_type(long int, char *); 
+5
source

I suggest bookmarking www.cdecl.org for questions of this type. Beware, the page is a little picky about what you enter (it does not accept parameter names at least).

+2
source

Start with the function name, then evaluate to using the usual expression syntax. Thus:

 proto_type // proto_type proto_type( long, char* ) // is a function taking long, char* // (since function call has precedence over * *proto_type( long, char* ) // returning a pointer (*proto_type(...)) // (no change, but necessary for precedence reasons) (...)( char const*, unsigned short ) // to a function taking char const*, unsigned short *(...)(...) // returning pointer void *(...)(...) // to void 

(This is a bit more complicated in cases where you don't have a name, like a type in static_cast .)

+2
source

I will explain my process for parsing complex c-style declarations. I hope you find this helpful.

C-style parsing can be considered as parsing from right to left, starting with an identifier and limited by brackets. So we start with the proto_type

 // vvvvvvvvvv------------------- void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d); 

As we see, moving directly from proto_type, we immediately see the function call and some parameters, so it's easy. proto_type is a function that takes long int and char* parameters.

 // v void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d); 

Here we get into the first bounding parentheses, so we need to go back to find the next token that is here:

 // v---------------------------- void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d); 

So now we know that it returns a pointer to something. Now we need to look right again to find out that due to bounding parentheses again

 // v--------------------------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d); 

Since we found another function call statement, the pointer returned by it should be a pointer to a function that accepts const char* and unsigned short int .

Finally, we have reached yet another bounding parenthesis, so go back to find the return value of this function pointer.

 //vvvv----------------------------------------------------------------------v void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d); 

The function pointer returns void * as the return value.

So, to return to English: proto_type takes long int and char* parameters as parameters and returns a pointer to a function that takes const char* and unsigned short int parameters as parameters and returns void* .

+2
source

proto_type is a function that takes two arguments: long int and char * . It returns a pointer to a function that takes two arguments of type const char * and unsigned short int .

And to write this without using typedef is not very happy with the reader.

+1
source

Using VS2012 on Windows 8 x64.

proto_type is a function that takes a long int (which is not called) and unsigned short int d , and returns a function of the form void * proto2(const char *b, unsigned short int d);

Here is the working code:

 #include <stddef.h> void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d); void * proto2(const char *b, unsigned short int d); int main(int argc, char** argv){ if(proto_type(0, NULL)(NULL, 0) != (void*)8675309) return 0; else return 1; } void * (*proto_type(long int, char *b)) (const char *b, unsigned short int d){ return &proto2; } void* proto2(const char *b, unsigned short int d){ return (void*)8675309; } 

What happens is that main calls proto_type , which returns a pointer to proto2 , which immediately calls main . proto2 returns the value of the magic pointer, which we check in main .

Here is the debug output of VS2012:

The program '[3188] test.exe' has exited with code 1 (0x1).

As for β€œhow this function works,” the answer, however, is what you want (if you implement it).

If you need another way to look at it, you want to read it inside to call the order of the functions.

The lesson here is that C (++) function pointers are very opaque.

+1
source

I did what @Tomek said, and copied your prototype to http: www.cdel.org, and then removed the parameter names from the prototype:

  void * (*proto_type(long int, char *)) (const char *, unsigned short int ); 

hit enter and the website came back:

 declare proto_type as function (long int, pointer to char) returning pointer to function (pointer to const char, unsigned short int) returning pointer to void 

Now, to translate this into a carbon block, we say: proto_type is declared as a function that takes a long int and a pointer to char parameters as parameters, returns a pointer to a function that takes a pointer to const char and an unsigned short integer , and this function returns void pointer .

Finally, a void pointer can be added to a pointer to any other data type.

+1
source

Source: https://habr.com/ru/post/1496860/


All Articles