1

There is a statement I saw in an C++ interview test today:

int (*(*fb)(int, char*))[2];

I have no idea what this declaration could mean. It looks much like function pointer but first star and square braces spoil everything.

Visual Studio decodes fb's type as following int[2] * (int, char *) *, which still looks like a bit cryptic.

If we simplify declaration than everything looks clear

int(*(*fa)(int, char*));

int* func(int, char*) 
{ 
    return 0; 
}

// now we can assign func to fa
fa = func;

Any ideas?

nikagra
  • 835
  • 2
  • 9
  • 23
  • 8
    [cdecl.org](http://cdecl.org) is your friend: `declare fb as pointer to function (int, pointer to char) returning pointer to array 2 of int` – Paul R Mar 02 '15 at 10:48
  • If this really is a C++ interview question, say that this is bad code, since nobody can read it intuitively. If the interviewer insists that this is good code, don't take the job. ;) Of course you should tell him how to write it instead, so throw the words type aliases, `std::function`, `std::vector` and `std::string` into the discussion. – leemes Mar 02 '15 at 11:02
  • 1
    See also this for how to read it: http://c-faq.com/decl/spiral.anderson.html – swalog Mar 02 '15 at 11:04
  • 4
    This sort of crap is why I hate interviews. Why should I waste my time memorising how to decrypt rubbish code like this? Give me a chance to show you what I can do with real code plzkthx. Beyond pointing out what leemes suggested to point out, there's nothing valuable with which you can answer this question. – Lightness Races in Orbit Mar 02 '15 at 11:04
  • There is some good information here. http://stackoverflow.com/questions/1591361/understanding-typedefs-for-function-pointers-in-c-examples-hints-and-tips-ple – user3528438 Mar 02 '15 at 12:42
  • @leemes In general, in well written code, I would expect some typedefs. On the other hand, in an interview, the goal is to determine whether the candidate really does know C++, and knowing how to read such declarations is part of knowing C++. Often,an interviewer will start with a statement to the effect that it is bad code, but he wants you to explain it anyway. And generally, he won't expect (and doesn't even want) an immediate answer; he wants you to talk through how you analyse it. – James Kanze Mar 02 '15 at 13:21
  • @LightnessRacesinOrbit Understanding real code is an important part of C++ programming, and there's a lot of real code out there that isn't well written. While I really wouldn't expect to see anything quite this bad in practice, in an interview, I would be interested in seeing how a candidate goes about analyzing it, and I certainly wouldn't hire a C++ programmer who didn't know where to start. (If he messes up in keeping track of the parentheses, or something like that, that's no big issue. But if he doesn't know the basic principles of reading a C++ declaration, it is.) – James Kanze Mar 02 '15 at 13:29
  • 2
    @JamesKanze: I would accept "I would go to cdecl.org" as a valid answer because it would show enough knowledge of what's going on and how to reach an answer. Though I do see your point. :) – Lightness Races in Orbit Mar 02 '15 at 16:26
  • @LightnessRacesinOrbit And I wouldn't, at least not if that is all of the answer. The goal of the question is to get some idea of the candidates understanding of how the C/C++ declaration syntax works. – James Kanze Mar 02 '15 at 17:39
  • @JamesKanze: Well I probably couldn't tell you off the bat what that declaration means, yet I'm a perfectly productive and decent software developer. So, I really don't think it's a valuable question. – Lightness Races in Orbit May 29 '17 at 23:43

3 Answers3

4

fb is a function pointer of the following signature:

  • The function takes two parameters: int and char*
  • The function returns a pointer to an array of two int, which has the type int(*)[2]

Usually, because of the cryptic syntax of function pointers, array pointers and such stuff, you should use typedefs or type aliases (the new using-syntax) to make it clearer step by step:

using int2 = int[2];
using int2ptr = int2*;
using fb = int2ptr(int, char*);

Proof

Also, instead of returning arrays, you could consider returning a std::vector or std::array; instead of passing char pointers you could consider std::string, and instead of using function pointers you could consider std::function. All these are "coulds", since every "raw type" has its reason to exist, but the reasons are very limited.

leemes
  • 44,967
  • 21
  • 135
  • 183
1

It is a definition of pointer to function that has two parameters, one of type int and other of type char *, and returns pointer to array of type int[2].

Here is a simplified demonstrative program. I have only changed the second parameter to type const char *

#include <iostream>

int(*f( int x, const char *s ))[2]
{
    static int a[2] = { x, *s };

    return &a;
}

int main()
{
    int (*(*fb)(int, const char*))[2] = f;
    auto p = fb( 10, "A" );

    std::cout << ( *p )[0] << '\t' << ( char )( *p )[1] << std::endl; 
    return 0;
}

The output is

10  A
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Colleague of mine have just sent an answer:

int (*(*fb)(int, char*))[2];

int(*(returnArray(int, char*)))[2]
{
    static int tab[2];
    return &tab;
}

// finally we have it
fb = returnArray;

I have no idea who can use this and for what purpose

nikagra
  • 835
  • 2
  • 9
  • 23