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


Groups > comp.lang.c > #19234

Re: Function prototype inside function prototyp

From nick_keighley_nospam@hotmail.com
Newsgroups comp.lang.c
Subject Re: Function prototype inside function prototyp
Date 2012-03-29 00:38 -0700
Organization http://groups.google.com
Message-ID <6870906.1996.1333006697138.JavaMail.geo-discussion-forums@ynim7> (permalink)
References <2f93aa41-3f37-4670-9515-7a44f0eeb9ec@wb9g2000pbc.googlegroups.com>

Show all headers | View raw


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...



Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web