2

(Sorry this is a sample quiz can't use typedef:P) I'm trying to write a declaration for the following function:

A function:

Accepts a pointer points to a function accepts a pointer to int and returns a pointer to int.

Returns a pointer to a function accepts int and returns int.

Here is my code:

int (* sigal(int *(*f)(int *)))(int);

However, this is a syntactical error. What's the right way to write it?


Edited:

The error seems to be there shouldn't be an f. I tried both my original code and

int (* sigal(int *(*)(int *)))(int);

on http://www.cdecl.org/. The later is passed.

Any explanation on what's the problem?


Update:

As 2501 said, the error seems to be a flavor of parser.

5
  • use a sequence of typedef's. This clarifies things a lot. Commented Apr 13, 2016 at 12:13
  • Make typedef for pointer to function and then use it for function definition. Commented Apr 13, 2016 at 12:13
  • 2
    This is a typical example of crap code. Commented Apr 13, 2016 at 12:14
  • Sorry this is a sample quiz can't use typedef Is the point of this quiz to teach people how not to write code? Commented Apr 13, 2016 at 12:51
  • @AndrewHenle I don't like it neither. Guess it's only to test how well students understand the syntax :D. Commented Apr 13, 2016 at 14:36

1 Answer 1

5

The correct way to write that is to use a typedef:

typedef int*(*fp)(int*);
typedef int(*fi)(int);

fi function(fp p);

The correct way to write this without typedef is:

int ( *( function( int*(*p)(int*) ) ) )(int);
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry it's a sample quiz from a course which can't use typedef :P. Would you please show me the full version?
thank you for your answer. But please don't worry it's a sample for mid-term quiz.
Sorry but I tried your answer in several parsers and still got a syntax error. What's the difference between yours and mine? Many thanks!
Sorry for the bother. Would you please see my update and make some explanations? Many thanks!
@spacegoing Don't rely on a single source of Truth.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.