5
double (*bar(int, double(*)(double,double[])))(double); 

While reviewing a lecture slide, I found an exercise left to the student:

In plain English, what is the type of bar in this C declaration?

Please help walk me through this. I don't even know where to begin, except that something is ultimately returning a double.

8
  • 1
    don't know what the type is, but suddenly I'm thinking Tim Hortons for some reason... Commented Nov 27, 2012 at 20:24
  • 1
    declare bar as function (int, pointer to function (double, array of double) returning double) returning pointer to function (double) returning double. (I cheated.) Commented Nov 27, 2012 at 20:25
  • (And that's a lot of double-talk.) Commented Nov 27, 2012 at 20:26
  • 3
    The first reaction should be to catch the poor bastard who codes like that, and yell at him in plain English :) Commented Nov 27, 2012 at 20:27
  • 1
    I'm not sure I fully understand why this question was closed as "too localized". While the exact line of code won't come up and need to be understood by most people, the ability to understand complex C declarations is a very useful skill... this question just exemplifies it. Commented Apr 14, 2014 at 16:19

2 Answers 2

10

If you're not sure you can always use the cdecl utility described in K&R like so:

$ cdecl Type `help' or `?' for help cdecl> explain double (*bar(int, double(*)(double,double[])))(double); declare bar as function (int, pointer to function (double, array of double) returning double) returning pointer to function (double) returning double 

So bar is a function that takes an int and a pointer to a function that takes a double and double[] and returns a double:

double(*)(double,double[])) 

And bar returns a pointer to another function that takes a double and returns a double

double(*)(double) 
Sign up to request clarification or add additional context in comments.

8 Comments

@Aerovistae If by "that" you mean the declaration from the slide, I guess it's there to subject students to a cruel and unusual punishment ;-)
Is it just me, or is the original code easier to read compared to that particular output of cdecl? :-P
@NikosC. No, the output of cdecl isn't easier to read/understand, since it is just another type of plaintext code and not human brain-native AST.
@NikosC. There's small choice in rotten apples :)
@Aerovistae Pick the answer that helped you most. That's what the checkmark is meant to indicate. Only if you pick a downvoted answer (and perhaps not even then if it's at -1) have you reason to stop and think whether you should really accept that one.
|
7

This answer is brought to you by the ability to use the Spiral Rule. Being able to understand a complex expression by starting at the unknown element and reading around it (resolving things in the parenthesis first). A very useful skill when reading code.

 bar - bar bar() - is a function bar(int, ) - which takes an int... bar(int, (*)()) - and a function pointer bar(int, double(*)()) - which returns a double bar(int, double(*)(double, )) - and takes a double... bar(int, double(*)(double, double[])) - and an array of doubles (*bar(int, double(*)(double, double[]))) - and returns a pointer (*bar(int, double(*)(double, double[])))() - to a function (*bar(int, double(*)(double, double[])))(double) - taking a double double(*bar(int, double(*)(double, double[])))(double) - which returns a double 

That was the hard way... There are of course sites that make this easier, the cdecl site for example; but it's good to be able to read code even when you can't get to the internet.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.