Ask your own question, for FREE!
Computer Science 11 Online
OpenStudy (anonymous):

Translate the following declarations into english: 1. char* const *(*next)(); 2. char *(*c[10])(int **p); 3. void (*(*f[])())()

OpenStudy (anonymous):

I can't believe they are all 'legal' in C

OpenStudy (anonymous):

Just like C++'s protected abstract virtual base pure virtual private destructors, I haven't found too many practical uses for such constructs :-P

OpenStudy (anonymous):

One more that is actually practical: void (*signal(int sig, void (*func) (int)) ) (int);

OpenStudy (anonymous):

If you're good at this, I recommend writing a program that translates C declarations into 'plain' English using your language of choice (C for me!).

OpenStudy (anonymous):

is it posssible ?

OpenStudy (anonymous):

Definitely. In fact, I believe that is already provided by some UNIX utility, and an exercise like this will illustrate some compiler elements such as parsing.

OpenStudy (anonymous):

http://www.mit.edu/~ocschwar/C_English.html

OpenStudy (anonymous):

I'll do the first one. next is a pointer to a function that returns a pointer to a read-only pointer to char.

OpenStudy (anonymous):

The second one: c is an array of size 10, of pointers to functions, that take a pointer to a pointer to an integer as the argument, that return pointers to char

OpenStudy (anonymous):

C is an easy language: the last one declares f, an array of unspecified size of pointers to functions that return pointers to functions that return nothing.

OpenStudy (anonymous):

so how do I write a program that does this stuff automatically?

OpenStudy (anonymous):

void (*signal(int sig, void (*func) (int)) ) (int); is a library header (I think in that signal.h) that defines signal as a function that returns pointers to functions that take an integer argument and that return nothing. The weird part is that one of signal's parameters has the same return type as signal: a pointer to a function that takes an integer and that returns nothing

OpenStudy (anonymous):

oops I meant "one of signal's arguments has the same type as signal's return value"

OpenStudy (anonymous):

typedef void (*pointer_to_function) (int)

OpenStudy (anonymous):

that way we can simplify signal's definition to: pointer_to_function signal(int sig, pointer_to_function)

OpenStudy (anonymous):

I've found an online version of cdecl: http://cdecl.org/

OpenStudy (anonymous):

Here's one: const int *(*s (void))(void)

OpenStudy (anonymous):

s is a function that returns pointers to functions that return pointers to readonly integers.

OpenStudy (anonymous):

or am I mistaken? Gotta write that program :(

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!