Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #19226 > unrolled thread

Function prototype inside function prototyp

Started bySam Hu <samhu.samhu@gmail.com>
First post2012-03-27 21:28 -0700
Last post2012-04-04 18:13 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.c


Contents

  Function prototype inside function prototyp Sam Hu <samhu.samhu@gmail.com> - 2012-03-27 21:28 -0700
    Re: Function prototype inside function prototyp nick_keighley_nospam@hotmail.com - 2012-03-29 00:38 -0700
    Re: Function prototype inside function prototyp Ben Pfaff <blp@cs.stanford.edu> - 2012-04-02 13:32 -0700
      Re: Function prototype inside function prototyp Sam Hu <samhu.samhu@gmail.com> - 2012-04-04 18:13 -0700

#19226 — Function prototype inside function prototyp

FromSam Hu <samhu.samhu@gmail.com>
Date2012-03-27 21:28 -0700
SubjectFunction prototype inside function prototyp
Message-ID<2f93aa41-3f37-4670-9515-7a44f0eeb9ec@wb9g2000pbc.googlegroups.com>
Greetings!

I've never seen this until now:

void thisworks(char c,int put(char ch,void* cl),void* cl)
{
      printf("This works!\n");
}

Why the function prototye int put(char ch,void* cl) can be in another
function declaration void thisworks(....) and where can I get more
information regarding this usage?

Grateful for your help in advance.

Regards,
Sam

[toc] | [next] | [standalone]


#19234

Fromnick_keighley_nospam@hotmail.com
Date2012-03-29 00:38 -0700
Message-ID<6870906.1996.1333006697138.JavaMail.geo-discussion-forums@ynim7>
In reply to#19226
On Wednesday, March 28, 2012 5:28:03 AM UTC+1, Sam Hu wrote:
> Greetings!
> 
> I've never seen this until now:
> 
> void thisworks(char c,int put(char ch,void* cl),void* cl)
> {
>       printf("This works!\n");
> }
> 
> Why the function prototye int put(char ch,void* cl) can be in another
> function declaration void thisworks(....) and where can I get more
> information regarding this usage?

so does this (to my surprise!)

**********************************
#include <stdio.h>

int myput (char ch, void* cl)
{
	printf ("ch is %c cl is %p\n", ch, cl);
	return ch;
}

void thisworks (char c, int put (char ch, void* cl), void* cl) 
{ 
	put (c, cl);
	printf ("This works!\n"); 
} 

int main (void)
{
	int i;
	thisworks ('a', myput, (void*)&i); 		
	return 0;
}
*************************

I must admit I'd expect this to work

void thisworks (char c, int (*put) (char ch, void* cl), void* cl) 

thus making put a function pointer. I'm guessing that's a compact version of a function pointer. or a compiler bug? I'm using Visual C++.

So even now C can still surprise me...



[toc] | [prev] | [next] | [standalone]


#19243

FromBen Pfaff <blp@cs.stanford.edu>
Date2012-04-02 13:32 -0700
Message-ID<87wr5xvpr6.fsf@blp.benpfaff.org>
In reply to#19226
Sam Hu <samhu.samhu@gmail.com> writes:

> I've never seen this until now:
>
> void thisworks(char c,int put(char ch,void* cl),void* cl)
> {
>       printf("This works!\n");
> }
>
> Why the function prototye int put(char ch,void* cl) can be in another
> function declaration void thisworks(....) and where can I get more
> information regarding this usage?

C99 says this:

     6.7.5.3  Function declarators (including prototypes)
...

8    A declaration of a parameter as ``function returning type''
     shall be adjusted to ``pointer to function returning type'',
     as in 6.3.2.1.

C89 said something similar, and I imagine that C11 does too.
-- 
Ben Pfaff 
http://benpfaff.org

[toc] | [prev] | [next] | [standalone]


#19346

FromSam Hu <samhu.samhu@gmail.com>
Date2012-04-04 18:13 -0700
Message-ID<16466028.531.1333588392644.JavaMail.geo-discussion-forums@vbab2>
In reply to#19243
On Tuesday, April 3, 2012 4:32:29 AM UTC+8, Ben Pfaff wrote:
> Sam Hu <samhu.samhu@gmail.com> writes:
> 
> > I've never seen this until now:
> >
> > void thisworks(char c,int put(char ch,void* cl),void* cl)
> > {
> >       printf("This works!\n");
> > }
> >
> > Why the function prototye int put(char ch,void* cl) can be in another
> > function declaration void thisworks(....) and where can I get more
> > information regarding this usage?
> 
> C99 says this:
> 
>      6.7.5.3  Function declarators (including prototypes)
> ...
> 
> 8    A declaration of a parameter as ``function returning type''
>      shall be adjusted to ``pointer to function returning type'',
>      as in 6.3.2.1.
> 
> C89 said something similar, and I imagine that C11 does too.
> -- 
> Ben Pfaff 
> http://benpfaff.org

Thank you so much.It is clear now.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web