Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: complex declarations |
| Date | 2014-03-04 15:49 -0500 |
| Organization | Self |
| Message-ID | <53163C3C.8060203@verizon.net> (permalink) |
| References | <lf5cdv$dll$1@dont-email.me> |
On 03/04/2014 03:18 PM, Robbie Brown wrote:
> I have the following code
>
> /* declare signal as function (int, pointer to function (int) returning
> void) returning pointer to function (int) returning void */
>
> void (*signal(int, void (*)(int)) ) (int);
>
> /* simplify things a bit using a typedef */
> typedef void (*fptr)(int);
>
> int main(void){
This location is inside the block associated with main.
> /* declare and define function that takes int and returns void */
> void foobar(int x){
> printf("%s%d\n", "x in foobar is ", x);
> }
C function definitions can only occur at file scope (6.9p1). You can
declare a function at block scope (6.8p2), but you can't define it
there. Because you can't define it with block scope, I also personally
feel it's a bad idea to declare it at file scope - but that's just a
personal preference, not a C rule.
If you got this code to compile (except for the part you're asking
about), then you're taking advantage of a non-conforming extension to C
which allows block scope definitions of functions. If making use of that
extension is important to you, then you should post your questions to a
forum specializing in the particular compiler that supports that
extension. If it supports that extension, it may support others, and
advice that you get from this newsgroup might be useless for that compiler.
> /* define signal using the typedef, compiles */
> fptr signal(int x, fptr y){
> return NULL;
> }
>
> /* What I'd like to do now is to replace the fptr typedef with the
> pre typedef type if that makes sense.
> Why? because it helps my understanding */
>
> /* Here I can say, void y(), void (y)(), void (*y)(), void y(int),
> void(y)(int) void (*y)(int) for the second arg and they all
> compile */
>
> fptr signal2(int x, void (*y)(int) ){
> NULL;
> }
>
> /* finally I want to do something like
> return a pointer to a function taking int and returning void
> but I just can't figure out how to do it. I've tried
> void (*)(int), void (int), (void (*) (int)) etc etc without
> success */
>
> /* can't get this to compile in any way */
> //void (*x) (int) signal3(int x, void (*y)(int) ){
That's because you start out declaring an identifier named x to be a
pointer to a function taking an argument of type int. Then you try to
follow that up with the declaration of signal3. You can declare the name
of a function and the names of it's arguments, but you can't declare a
name for the value returned by that function. What you want to do is to
declare signal3 as returning an UNNAMED pointer to a function taking one
argument of type int.
> // return NULL;
> //}
>
> }
>
>
> How do I code signal3 to completely get rid of my dependence on the
> typedef. Once again, the only reason I want to do this is because I can
> (hopefully)
How do you make the pointer returned by signal3 unnamed? You have to use
a type name, which syntactically looks just like the declaration of an
identifier of a function or an object, except that it omits the identifier.
void (*)(int) signal3(int x, void(*y)(int))
Type names are also used in casts and sizeof() expressions. In C99,
they're also used in compound literals. In C2011, they're also used in
_Generic() associations, _Alignof() expressions, _Atomic() type
specifiers, and _Alignas() specifiers.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-04 20:18 +0000
Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-04 15:49 -0500
Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-04 16:06 -0500
Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-05 12:18 +0000
Re: complex declarations "BartC" <bc@freeuk.com> - 2014-03-05 12:56 +0000
Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-05 08:24 -0500
Re: complex declarations "BartC" <bc@freeuk.com> - 2014-03-05 16:26 +0000
Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-05 08:13 -0500
Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-05 13:32 +0000
Re: complex declarations Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-05 14:32 +0000
Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-04 21:42 +0000
Re: complex declarations Keith Thompson <kst-u@mib.org> - 2014-03-04 14:04 -0800
Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-05 09:22 +0000
Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-05 07:29 -0500
Re: complex declarations Keith Thompson <kst-u@mib.org> - 2014-03-05 08:38 -0800
Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-04 17:05 -0500
Re: complex declarations Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-04 21:15 +0000
Re: complex declarations "BartC" <bc@freeuk.com> - 2014-03-04 21:38 +0000
Re: complex declarations Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-04 23:27 +0000
Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-04 21:59 +0000
Re: complex declarations James Kuyper <jameskuyper@verizon.net> - 2014-03-04 17:12 -0500
Re: complex declarations Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-05 00:28 +0000
Re: complex declarations Robbie Brown <dac@nomail.invalid> - 2014-03-05 09:10 +0000
Re: complex declarations Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-05 11:44 +0000
csiph-web