0

This is one of the problems in my homework. I'm asked to describe the type of x as it is specified by the following declarations:

 a) char (*x[10]) (int);
 b) int (*x(int)) [5];
 c) float *(*x(void)) (int);
 d) void (*x(int, void (*y) (int))) (int);

I understand A) and can kinda make out what B) is , but C) and D) just look completely too cryptic for me.

Are these sort of declarations common in C?

Jules
  • 423
  • 3
  • 11
  • Explain what you understand. These declarations all serve a purpose. They will appear whenever there is a use case for them. – Mad Physicist Nov 06 '19 at 02:33
  • I wouldn't say they're common, but the ability to read them is a useful skill, because if you know how to read them, you'll be able to properly understand the declarations that *are* common and that you do use. – Steve Summit Nov 06 '19 at 02:36
  • 2
    See [How do you read C declarations?](https://stackoverflow.com/questions/89056/how-do-you-read-c-declarations). – Steve Summit Nov 06 '19 at 02:38
  • If your assignment asks you to *describe the type of x as it is specified in the following declarations*, what difference does it make how common those declarations are? You're still being asked to solve the same problem whether they're common or not. – Ken White Nov 06 '19 at 02:40
  • 2
    @paddy: Asking questions about homework is suitable for Stack Overflow. One should not ask for an entire homework problem to be done, but asking about particular points is accepted and proper. And this question does not ask about the homework, it asks about information the poster was inspired to ponder by the homework assignment. – Eric Postpischil Nov 06 '19 at 02:52
  • These declarations may appear complicated, but they are all built up using a small number of rules. Just strip off one layer at a time to understand them, like peeling an onion. – Tom Karzes Nov 06 '19 at 03:09

1 Answers1

0

Declaration (a) (array of function pointers, probably serving as some kind of dispatch table) is not unheard of. The others are not very common and if anything like them occurs, it will likely use typedef names.

I wrote some code long ago in which there were some character arrays defined as typedef names, and these were passed into and returned from functions:

typedef char fixed_string[256];

fixed_string *fun(fixed_string *);

If we don't use a typedef for the function declaration, it looks like:

char (*fun(char (*)[256]))[256];

Using arrays this way is inadvisable; the non-typedef version is hard to read and the typedefs are a leaky abstraction, because the arrays still undergo conversion to pointers. I wouldn't write that today.

Fixed arrays that are passed around are best wrapped up into a struct:

struct fixed_string { char s[256]; };

we can pass and return that by value, or using pointers.

The (d) case:

void (*x(int, void (*y) (int))) (int);

can be rewritten with typedefs like this:

typedef void (*action)(int);

action x(int, action);

This hints at pattern that can occur in some kinds of state machines. Maybe x calls that action, passing it the int parameter, and then returns a pointer to another function that serves as the next action.

Kaz
  • 55,781
  • 9
  • 100
  • 149