473,765 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Weird bit of code-- legal???

A while back I had to recompile some old code, originally written by a
really good programmer, but one prone to use every trick in the book,
and then some.

There was a statement, something like this:
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.

Sep 13 '06 #1
29 2577

Ancient_Hacker wrote:
A while back I had to recompile some old code, originally written by a
really good programmer, but one prone to use every trick in the book,
and then some.

There was a statement, something like this:
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.
This code is really quite simple and shouldn't give a decent C
programmer any problems whatsoever.

The line is basically a function call that puts the result in Result.
Depending on the value of Mode (is it 2?) it will invoke the function
pointed to by TwoWayFunc or FourWayFunc; passing the Params 1 through
3.

Why shouldn't it be legal and why would it cause any head scratching?

Sep 13 '06 #2

gbost...@excite .com wrote:
Ancient_Hacker wrote:
A while back I had to recompile some old code, originally written by a
really good programmer, but one prone to use every trick in the book,
and then some.

There was a statement, something like this:
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.

This code is really quite simple and shouldn't give a decent C
programmer any problems whatsoever.

The line is basically a function call that puts the result in Result.
Depending on the value of Mode (is it 2?) it will invoke the function
pointed to by TwoWayFunc or FourWayFunc; passing the Params 1 through
3.

Why shouldn't it be legal and why would it cause any head scratching?

.... because I didnt mention, the function names are just that-- not
variables holding the function addresses. I did not know you could
use raw function names in a conditional expression. Then again, I
didnt know and I suspect a lot of folks don't know that the thing
before the parameters is an actual "expression ".

Sep 13 '06 #3
Ancient_Hacker posted:
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C?

Not to boast, but I understood the line of code at first glance.

"TwoWayFunc " and "FourWayFun c" are pressumably both function pointers of the
same type.

"Mode == 2" is evaluated, and depending on the result, either of the
functions is called with the arguments, "Param1, Param2, Param3".

The return value of the function call is stored in "Result".

The code is perfectly legal.

--

Frederick Gotham
Sep 13 '06 #4
Ancient_Hacker posted:
... because I didnt mention, the function names are just that-- not
variables holding the function addresses. I did not know you could
use raw function names in a conditional expression.

When you use the name of a function on its own (i.e. without parenthese after
it which contain arguments), then what you have is the address of that
function.

For instance, let's define a function:

void Func1(void) {}

Now let's create a pointer to a function of that type:

void (*p)(void);

Now let's store the address of Func1 in p:

p = Func1;

The expression "Func1" in the above is the address of the function.

--

Frederick Gotham
Sep 13 '06 #5
In article <11************ **********@p79g 2000cwp.googleg roups.com>,
<gb******@excit e.comwrote:
....
>There was a statement, something like this:
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.

This code is really quite simple and shouldn't give a decent C
programmer any problems whatsoever.
It is not a question of "understand ing" it or whether or not it works on
any given implementation. As you well know, both of those topics are OT
in this ng.

Rather, it is a question of whether or not it is "strictly conforming",
according to the gods of the ng. I remember reading here sometime back
that there are some funky rules regarding "?:" expressions in the
standard - of the usual sort, that is, that allow to fail things that
obviously should work and which do work on most-if-not-all implementations .

And which are thus, by the rules of the NG,
OT/non-portable/wrong/can't-discuss-here/blah-blah-blah.

Sep 13 '06 #6

Frederick Gotham wrote:
Not to boast, but I understood the line of code at first glance.

"TwoWayFunc " and "FourWayFun c" are pressumably both function pointers of the
same type.
That's the funny thing, they're not ptrs. They're the raw function
names. I'd never seen conditional expressions used in that particular
context.

Sep 13 '06 #7
Ancient_Hacker wrote:
Frederick Gotham wrote:
>Not to boast, but I understood the line of code at first glance.

"TwoWayFunc " and "FourWayFun c" are pressumably both function pointers of the
same type.

That's the funny thing, they're not ptrs.
Doesn't that make them constant pointers? Or are they different?

Thumbing through K&R2 to try to answer this myself.
Sep 13 '06 #8
Ancient_Hacker said:
>
Frederick Gotham wrote:
>Not to boast, but I understood the line of code at first glance.

"TwoWayFunc " and "FourWayFun c" are pressumably both function pointers of
the same type.

That's the funny thing, they're not ptrs.
Yes, they are.
They're the raw function names.
In other words, they're pointers.

Look up "function designator" in the Standard.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 13 '06 #9
Ancient_Hacker wrote:
Frederick Gotham wrote:
>Not to boast, but I understood the line of code at first glance.

"TwoWayFunc " and "FourWayFun c" are pressumably both function pointers of the
same type.

That's the funny thing, they're not ptrs. They're the raw function
names. I'd never seen conditional expressions used in that particular
context.
C99, 6.3.2 Other operands
4. A function designator is an expression that has function type.
Except when it is the
operand of the sizeof operator or the unary & operator, a
function designator with
type ‘‘function returning type’’ is converted to an expression
that has type ‘‘pointer to
function returning type’’.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Sep 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1909
by: SenderX | last post by:
I am a basically a life-long Win32 C programmer specializing in high-end winsock servers. Before I dive into portable C++ programming, I wanted to code up an example of what I thought would be a workable Collection template library. The idea is to design API's that operate on interfaces, and provide multi-implementations to that "single" API. C++ Interfaces and Implementations, seems to behave like Win32 COM objects?
9
1479
by: Marco Jez | last post by:
Is it legal, in a class template's member function, to instantiate the same class template but with a different type as argument? The following code is very simple and I'd expect it to compile, but it makes both MSVC and gcc freeze and never return. Sometimes I get an "internal compiler error" instead. struct Instance_base { virtual ~Instance_base() {} virtual Instance_base* mkptr() = 0;
8
1436
by: Deano | last post by:
Here's the code; Private Sub txtTeachName_LostFocus() If IsNull(Me.txtName) Then 'line A Forms!frmMainform!frmSubform.Locked = True GoTo Exit_txtName Else 'line B Forms!frmMainform!frmSubform.Locked = False 'line C
30
2139
by: Kiuhnm | last post by:
#include <new> class T { }; int main() { T t = t; T u(u);
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.