By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,085 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

Weird bit of code-- legal???

P: n/a
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
Share this Question
Share on Google+
29 Replies


P: n/a

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

P: n/a

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

P: n/a
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 "FourWayFunc" 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

P: n/a
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

P: n/a
In article <11**********************@p79g2000cwp.googlegroups .com>,
<gb******@excite.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 "understanding" 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

P: n/a

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

"TwoWayFunc" and "FourWayFunc" 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

P: n/a
Ancient_Hacker wrote:
Frederick Gotham wrote:
>Not to boast, but I understood the line of code at first glance.

"TwoWayFunc" and "FourWayFunc" 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

P: n/a
Ancient_Hacker said:
>
Frederick Gotham wrote:
>Not to boast, but I understood the line of code at first glance.

"TwoWayFunc" and "FourWayFunc" 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

P: n/a
Ancient_Hacker wrote:
Frederick Gotham wrote:
>Not to boast, but I understood the line of code at first glance.

"TwoWayFunc" and "FourWayFunc" 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

P: n/a
Kenny McCormack wrote:
>
In article <11**********************@p79g2000cwp.googlegroups .com>,
<gb******@excite.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 "understanding" 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.
What's OT? What's non-portable? What's wrong? What's non-discussable
here?

Now, the post is missing the definitions for all referenced names, but
assuming that TwoWayFunc and FourWayFunc are functions (or pointers to
functions) with the same prototype, there's nothing wrong here.

And, given that the OP's question was basically "is this legal C, or
an extension to this particular compiler", I would say that the OP is
100% on-topic.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Sep 13 '06 #11

P: n/a
"Ancient_Hacker" <gr**@comcast.netwrites:
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.
Yes, assuming TwoWayFunc and FourWayFunc are functions of the same
type, and that the parameter list is valid for both.

Expressions of function type (not pointer-to-function type, function
type) are treated similarly to expressions of array type. An
expression of function type (such as the name of a function) is
implicitly converted to a pointer to the function, unless it's the
operand of a unary "sizeof" or "&" operator. (If it's the operand of
sizeof, it's a constraint violation; this rule makes "sizeof func"
illegal rather than an expression that yields the size of a function
pointer. If it's the operand of "&", the result is the address of the
function, the same result you'd get without the "&".)

An ordinary function call:
func(arg1, arg2)
invokes the function call operator. The left operand must be a
pointer-to-function. The conversion of "func" to a pointer occurs
before the function call operator is evaluated.

If func_ptr is an object of type pointer-to-function then this:
func_ptr(arg1, arg2)
works the same way, except that no implicit conversion was done on the
left operand.

In your expression, if TwoWayFunc and FourWayFunc are function names,
they're both implicitly converted to function pointers. The
expression ( Mode == 2 ? TwoWayFunc : FourWayFunc ) is evaluated
normally, and the result (either a pointer to TwoWayFunc or a pointer
to FourWayFunc) is used as the left operand of the function call
operator.

Once you understand how function names are handled, and how the
function call operator works, it's all perfectly straightforward.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 13 '06 #12

P: n/a
Kenneth Brody <ke******@spamcop.netwrites:
Kenny McCormack wrote:
[the usual]
What's OT? What's non-portable? What's wrong? What's non-discussable
here?
Kenny McCormack is a self-proclaimed troll. Please ignore him. I
recommend killfiling him; you won't miss anything useful or
interesting.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 13 '06 #13

P: n/a
Frederick Gotham wrote:
>
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?
The code is perfectly legal.
I've posted code like that.

http://groups.google.com/group/comp....ff120bfca2cac1

string = (digit == 0 ? sput_ip1 : sput_i)(integer / 10, string);

I've subsequently rewritten it,
so that it doesn't look like that anymore.

--
pete
Sep 13 '06 #14

P: n/a
In article <45**************@spamcop.net>,
Kenneth Brody <ke******@spamcop.netwrote:
....
>It is not a question of "understanding" 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.

What's OT? What's non-portable? What's wrong? What's non-discussable
here?
The phrases (which I myself introduced):
obviously should work and which do work on most-if-not-all implementations.
>Now, the post is missing the definitions for all referenced names, but
assuming that TwoWayFunc and FourWayFunc are functions (or pointers to
functions) with the same prototype, there's nothing wrong here.

And, given that the OP's question was basically "is this legal C, or
an extension to this particular compiler", I would say that the OP is
100% on-topic.
Oh, I agree. I wasn't saying that the post was OT. I was saying that
what the OP was trying to get at was: Is it legal C or is it OT (I.e.,
happens to work on my implementation).

Oh, and pay no attention to the real trolls here - the so-called regs
who go into a tizzy every time I post. They're harmless once you get
used to it.

Sep 14 '06 #15

P: n/a

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.
IMHO function decay is one of the most beautiful things in all of C.
For one thing, and many people don't realize this, it practically makes
C object oriented, in the hands of a skilful user. In fact, it allows
all the benefits of object orientation without any of the drawbacks. :)

Sep 14 '06 #16

P: n/a
Kenneth Brody wrote:
<gb******@excite.comwrote:
>>
> Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
>Param2, Param3 );

Now, the post is missing the definitions for all referenced names, but
assuming that TwoWayFunc and FourWayFunc are functions (or pointers to
functions) with the same prototype, there's nothing wrong here.
The functions don't have to have prototypes; this is accepted
by my compiler:

int foo();
int bar(int x) { return 0; }

int main()
{
return (0 ? foo : bar)(1);
}

int foo() { }

In fact, this is also accepted and runs correctly, although I'm not
sure how well-defined it is:

#include <stdio.h>

int foo();

int bar(char *x) {
printf("bar: %p\n", x);
return 0;
}

int main()
{
return (getchar() == 'x' ? foo : bar)(0);
}

int foo(int x) {
printf("foo: %d\n", x);
return 0;
}

Sep 14 '06 #17

P: n/a
On 13 Sep 2006 08:09:05 -0700, "Ancient_Hacker" <gr**@comcast.net>
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 is equivalent to:

if ( Mode == 2 )
{
Result = TwoWayFunc(Param1, Param2, Param3);
}
else
{
Result = FourWayFunc(Param1, Param2, Param3);
}

The original "really good programmer" bit off more than he could chew,
from the standpont if understandability and maintainability, as the
equivalent code should not cause your head to scratch at all.

In the world of safety critical software, which I deal with on a
regular basis, we have no tolerance for such code as that written by
the "really good programmer". He or she either changes it or he or she
is out the door. No if, ands, or but about it.

Best regards
--
jay
Sep 14 '06 #18

P: n/a
Ancient_Hacker wrote:
>
Frederick Gotham wrote:
>Not to boast, but I understood the line of code at first glance.

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

That's the funny thing, they're not ptrs. They're the raw function
names.
(fx:blink) Yes, which decay to pointers in just about any legal context.

I suppose C /could/ have been defined to require using &functionName to
get the associated pointer. Let us be thankful for small mercies.

--
Chris "&co" Dollin
A rock is not a fact. A rock is a rock.

Sep 14 '06 #19

P: n/a
Kenny McCormack wrote:
>
In article <45**************@spamcop.net>,
Kenneth Brody <ke******@spamcop.netwrote:
...
It is not a question of "understanding"
it or whether or not it works on
any given implementation. As you well know,
both of those topics are OT in this ng.
C code, is on topic.
The question of whether or not expressions of function type
can be used as the second and third expressions
of the conditional operator,
is on topic.

>
Rather, it is a question of whether or not
it is "strictly conforming",
according to the gods of the ng.
"Correct code" is on topic.
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.
You remember nothing.
And which are thus, by the rules of the NG,
OT/non-portable/wrong/can't-discuss-here/blah-blah-blah.
What's OT? What's non-portable? What's wrong?
What's non-discussable here?
Kenny can't remember anything.
If he could, then he would remember that he's always wrong
when he tries to be topical on this newgroup,
and that that's why he decided to become a troll.

--
pete
Sep 14 '06 #20

P: n/a
Keith Thompson wrote:
>
Kenneth Brody <ke******@spamcop.netwrites:
Kenny McCormack wrote:
[the usual]
What's OT? What's non-portable? What's wrong? What's non-discussable
here?

Kenny McCormack is a self-proclaimed troll. Please ignore him. I
recommend killfiling him; you won't miss anything useful or
interesting.
Yes, but at least his "blah blah blah" posts are usually in response
to things that could be considered OT.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Sep 14 '06 #21

P: n/a
Old Wolf wrote:
>
Kenneth Brody wrote:
<gb******@excite.comwrote:
>
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );
Now, the post is missing the definitions for all referenced names, but
assuming that TwoWayFunc and FourWayFunc are functions (or pointers to
functions) with the same prototype, there's nothing wrong here.

The functions don't have to have prototypes; this is accepted
by my compiler:

int foo();
int bar(int x) { return 0; }

int main()
{
return (0 ? foo : bar)(1);
}

int foo() { }
AFAIK, the above invokes UB, because you're calling a function
without a prototype in scope (foo) with the wrong parameters.

However, given that your compiler _probably_ uses the "caller
cleans up the stack" convention, the odds are that it will, in
fact, "work" on many platforms. (By "work", I mean "give the
expected results".)
In fact, this is also accepted and runs correctly, although I'm not
sure how well-defined it is:

#include <stdio.h>

int foo();

int bar(char *x) {
printf("bar: %p\n", x);
return 0;
}

int main()
{
return (getchar() == 'x' ? foo : bar)(0);
}

int foo(int x) {
printf("foo: %d\n", x);
return 0;
}
I will probably fail miserably on platforms with:

all-bits-zero being a trap representation for a pointer
or
sizeof(char *) != sizeof(int)

And, I assume that if you place the actual definition of foo()
above main, rather than the "function that takes an unknown
set of arguments" type, that the compiler complains about a
mismatch between foo() and bar()?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Sep 14 '06 #22

P: n/a
jaysome wrote:
On 13 Sep 2006 08:09:05 -0700, "Ancient_Hacker" <gr**@comcast.net>
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 is equivalent to:

if ( Mode == 2 )
{
Result = TwoWayFunc(Param1, Param2, Param3);
}
else
{
Result = FourWayFunc(Param1, Param2, Param3);
}
Hey, that looks like a good job for a conditional expression!

result = Mode == 2 ? TwoWayFunc(Param1, Param2, Param3) :
FourWayFunc(Param1, Param2, Param3);

I presume nobody's scratching their head here. Whether you do that with an
if-statement or a conditional depends on how comfortable you are with
stuffing things on one line. We're assuming, of course, that "Param1" and
friends really are called that, and are not placeholders for two mile long
expressions with side effects.

But hold on, is "2" a magic value here? Wouldn't a variable called "Mode"
hold some sort of state parameter? Well then

switch (Mode) {
case 2: TwoWayFunc(Param1, Param2, Param3); break;
default: FourWayFunc(Param1, Param2, Param3); break;
}

is a nice way of emphasizing this. Now, "2" shouldn't be a magic value, so
toss in a #define or make Mode of enum type, but that's changing around
semantics.
The original "really good programmer" bit off more than he could chew,
from the standpont if understandability and maintainability, as the
equivalent code should not cause your head to scratch at all.

In the world of safety critical software, which I deal with on a
regular basis, we have no tolerance for such code as that written by
the "really good programmer". He or she either changes it or he or she
is out the door. No if, ands, or but about it.
Must be nice to be able to cull the herd like that. I have to take the
occasional clue-by-four to my colleagues when I get called in to fix a
problem that's ultimately the result of bozotic code... admittedly not in C,
I don't think they're ready for that. ("What's wrong with an empty exception
handler? I really want to ignore errors here!")

S.
Sep 14 '06 #23

P: n/a

jaysome wrote:
On 13 Sep 2006 08:09:05 -0700, "Ancient_Hacker" <gr**@comcast.net>
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 is equivalent to:

if ( Mode == 2 )
{
Result = TwoWayFunc(Param1, Param2, Param3);
}
else
{
Result = FourWayFunc(Param1, Param2, Param3);
}

The original "really good programmer" bit off more than he could chew,
from the standpont if understandability and maintainability, as the
equivalent code should not cause your head to scratch at all.
The original code shouldn't have caused anyone to do any head
scratching either, unlesss they are a junior level programmer who needs
to get up to speed in a hurry.
>
In the world of safety critical software, which I deal with on a
regular basis, we have no tolerance for such code as that written by
the "really good programmer". He or she either changes it or he or she
is out the door. No if, ands, or but about it.
So the world in which you deal is loaded with junior programmers who
have no business being anywhere near safety critical software. Dumbing
down the coders is not the solution. You need better programmers.
>
Best regards
--
jay
Sep 14 '06 #24

P: n/a
jaysome wrote:
> 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 is equivalent to:

if ( Mode == 2 )
{
Result = TwoWayFunc(Param1, Param2, Param3);
}
else
{
Result = FourWayFunc(Param1, Param2, Param3);
}
It isn't exactly equivalent. In your version of the conditional
expression, you can put functions prototyped with any parameters, not
only functions which take three parameters of the types of Param1,
Param2, Param3.

The original example in the compact syntax actually puts a compile-time
constraint on this, and your "equivalent" syntax does not.

Sep 14 '06 #25

P: n/a
jaysome wrote:
In the world of safety critical software, which I deal with on a
regular basis, we have no tolerance for such code as that written by
the "really good programmer". He or she either changes it or he or she
is out the door. No if, ands, or but about it.
If I told you that the compact syntax made it possible for us to
continue using a code generator, or if I told you it helped
significantly in debugging, or something of that nature, you'd still
show me the door? After I'd been working for you for 12 years?

Please think before you go off about how low your standards are for
firing someone. Nobody who had a choice, would want to work for you if
you really had a policy like that.

In another post, I gave you a reason why your "equivalent" code isn't
exactly. Of course, the difference between code you got from me, and
the code in the example, is that my code would have comments.

Forgive me, I haven't had my coffee today, and as it turns out, I'm
rewriting someone else's bad code this morning.

James
Sep 14 '06 #26

P: n/a
pete <pf*****@mindspring.comwrites:
[...]
Kenny can't remember anything.
If he could, then he would remember that he's always wrong
when he tries to be topical on this newgroup,
and that that's why he decided to become a troll.
And that's why *he should be ignored*. Please stop feeding the troll.
And please ignore whatever idiocy he posts in response to this.

I recommend killfiling him. You won't miss anything useful or
interesting.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 14 '06 #27

P: n/a
Kenneth Brody wrote:
Old Wolf wrote:

The functions don't have to have prototypes; this is accepted
by my compiler:

int foo();
int bar(int x) { return 0; }

int main()
{
return (0 ? foo : bar)(1);
}

int foo() { }

AFAIK, the above invokes UB, because you're calling a function
without a prototype in scope (foo) with the wrong parameters.
Well, foo is never called in the above example.
In fact, this is also accepted and runs correctly, although I'm not
sure how well-defined it is:

#include <stdio.h>

int foo();

int bar(char *x) {
printf("bar: %p\n", x);
return 0;
}

int main()
{
return (getchar() == 'x' ? foo : bar)(0);
}

int foo(int x) {
printf("foo: %d\n", x);
return 0;
}

I will probably fail miserably on platforms with:

all-bits-zero being a trap representation for a pointer
or
sizeof(char *) != sizeof(int)
Why does the compiler decide that 0 is a pointer and
not an integer, in the case of calling foo?
>
And, I assume that if you place the actual definition of foo()
above main, rather than the "function that takes an unknown
set of arguments" type, that the compiler complains about a
mismatch between foo() and bar()?
Yes, because the definition of foo serves as a prototype.

Sep 15 '06 #28

P: n/a
On Thu, 14 Sep 2006 07:59:50 -0700, jmcgill
<jm*****@email.arizona.eduwrote:
>jaysome wrote:
>> 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 is equivalent to:

if ( Mode == 2 )
{
Result = TwoWayFunc(Param1, Param2, Param3);
}
else
{
Result = FourWayFunc(Param1, Param2, Param3);
}

It isn't exactly equivalent. In your version of the conditional
expression, you can put functions prototyped with any parameters, not
only functions which take three parameters of the types of Param1,
Param2, Param3.

The original example in the compact syntax actually puts a compile-time
constraint on this, and your "equivalent" syntax does not.
If function prototypes are required, I don't see how your argument
holds. This is valid C:

int TwoWayFunc(int,int,int);
int FourWayFunc(int,int,int);
int TwoWayFunc_v(int, ...);
int FourWayFunc_v(int, ...);
int Mode;
int Result;
int main(void)
{
int Param1 = 0;
int Param2 = 0;
int Param3 = 0;
Result = (Mode == 2 ? TwoWayFunc : FourWayFunc ) (Param1,
Param2, Param3 );
Result = (Mode == 2 ? TwoWayFunc_v : FourWayFunc_v) (Param1,
Param2, Param3 );
return 0;
}

Best regards
--
jay
Sep 15 '06 #29

P: n/a
On Thu, 14 Sep 2006 08:13:16 -0700, jmcgill
<jm*****@email.arizona.eduwrote:
>jaysome wrote:
>In the world of safety critical software, which I deal with on a
regular basis, we have no tolerance for such code as that written by
the "really good programmer". He or she either changes it or he or she
is out the door. No if, ands, or but about it.

If I told you that the compact syntax made it possible for us to
continue using a code generator, or if I told you it helped
significantly in debugging, or something of that nature, you'd still
show me the door? After I'd been working for you for 12 years?
When I referred to that "really good programmer", I assumed it was
clear that I was talking about a real person, not some robotic code
generator. Nevertheless, even a code generator has to be programmed by
some real person. And the nature of the code that said code generator
generates says a lot about the person programming the code generator.

In this case, I cannot think of any good reason why such a code
generator would generate code like this, let alone how such code could
"significantly" help in debugging. In the world of safety critical
software, tools like code generators may need to be verified as if a
real person wrote the code.
>Please think before you go off about how low your standards are for
firing someone. Nobody who had a choice, would want to work for you if
you really had a policy like that.
Of course.
>In another post, I gave you a reason why your "equivalent" code isn't
exactly. Of course, the difference between code you got from me, and
the code in the example, is that my code would have comments.
I'm skeptical that you did. But comments are, of course, good.
>Forgive me, I haven't had my coffee today, and as it turns out, I'm
rewriting someone else's bad code this morning.
Forgive me too. I may not have had my coffee today either :^)

Best regards
--
jay

Sep 15 '06 #30

This discussion thread is closed

Replies have been disabled for this discussion.