LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2013, 02:35 PM   #1
Gullible Jones
Member
 
Registered: Apr 2011
Posts: 142

Rep: Reputation: 10
Any (standard) way to have anonymous functions in C?


I have a program I'm working on, in which I'd like to have structures containing pointers to anonymous functions. Basically the structure type would contain a function pointer as a member; and when each individual structure was initalized, I would feed the address of an anonymous function into its pointer.

This way I could have some degree of polymorphism; i.e. I could call the correct function required by a given structure instance, without needing different function names, switch statements, etc. Instead of calling foo() for a foo_s struct and bar() for a bar_s struct, I could call (some_foo->func)() or (some_bar->func)().

Is there a sane, commonplace way to do this in C? Is it even possible without implementing half of C++? Failing that, are there better ways ot have this kind of polymorphism?
 
Old 05-04-2013, 06:17 PM   #2
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
http://stackoverflow.com/questions/8...ters-in-c-work
 
Old 05-04-2013, 06:43 PM   #3
Gullible Jones
Member
 
Registered: Apr 2011
Posts: 142

Original Poster
Rep: Reputation: 10
Thanks very much, but that doesn't seem to help a whole lot. With just function pointers, I would still have to implement a large number of named functions.

Is there any way to create anonymous functions in C, that is not dependent on the compiler?
 
Old 05-04-2013, 06:47 PM   #4
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
You still have to write the functions anyway. You could call them classA_function1, classA_function2, classB_function1 etc. If you really want them to be anonymous then you might as well use C++.
 
Old 05-04-2013, 06:50 PM   #5
Gullible Jones
Member
 
Registered: Apr 2011
Posts: 142

Original Poster
Rep: Reputation: 10
I wanted them to be anonymous because that could allow for prettier, more readable code. (Or so I thought anyway.) I can't use C++ without porting the entire existing code base.

Thanks though!

Last edited by Gullible Jones; 05-04-2013 at 06:51 PM.
 
Old 05-04-2013, 06:57 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
No, there is no standard way to have anonymous functions in C.

The Wikipedia article on anonymous functions does have a gcc example though.

http://en.wikipedia.org/wiki/Anonymo...da_expressions

Quote:
This way I could have some degree of polymorphism; i.e. I could call the correct function required by a given structure instance, without needing different function names, switch statements, etc. Instead of calling foo() for a foo_s struct and bar() for a bar_s struct, I could call (some_foo->func)() or (some_bar->func)().
You don't need anonymous functions for that.

Last edited by dugan; 05-04-2013 at 07:11 PM.
 
Old 05-04-2013, 07:16 PM   #7
Gullible Jones
Member
 
Registered: Apr 2011
Posts: 142

Original Poster
Rep: Reputation: 10
I know I don't need them, it just seemed like a good idea for keeping the code more compact.
 
Old 05-05-2013, 08:02 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Gullible Jones View Post
I have a program I'm working on, in which I'd like to have structures containing pointers to anonymous functions. Basically the structure type would contain a function pointer as a member; and when each individual structure was initalized, I would feed the address of an anonymous function into its pointer.

This way I could have some degree of polymorphism; i.e. I could call the correct function required by a given structure instance, without needing different function names, switch statements, etc. Instead of calling foo() for a foo_s struct and bar() for a bar_s struct, I could call (some_foo->func)() or (some_bar->func)().

Is there a sane, commonplace way to do this in C? Is it even possible without implementing half of C++? Failing that, are there better ways ot have this kind of polymorphism?

C99 standard does not have anonymous functions.
 
Old 05-06-2013, 01:31 AM   #9
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Others have already answered your question so I'll just add that the usual way is to declare those functions static so they are not exported to other translation units. But yes, you'll have to give them names, but as psionl0 pointed, this does not have to be that bad since they can be named after the callback name.
 
Old 05-06-2013, 02:54 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I do not really understand it, it is exactly how c++ works: you will have classes, member functions, you can also have virtual functions, and inheritance and finally this will do exactly what you need.
You could call always the correct function because every class instance will "know" himself.
I do not really understand why do you want to build a similar structure (reinvent the wheel?), it is already working and available.

Otherwise (without C++, in c) you must implement all those functions one by one with some names. You can than put them (or their pointers) in an array, shared lib or whatever you want, and use them.

The third possibility could have been a c++ to c translator...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Underscores in standard functions, an example section from cat.c is included. pr_deltoid Programming 1 09-04-2010 10:49 PM
Problems with anonymous login proftp 1.3.1: 530-Unable to set anonymous priviliges. gentooox Linux - Server 3 05-03-2009 02:46 PM
Profiling Standard C library functions using gprof ecooper Programming 1 01-08-2007 09:46 AM
Console standard iInput functions yrauchwerger Programming 1 05-23-2006 02:52 AM
Standard C library numeric array functions? bulliver Programming 8 04-12-2006 08:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration