Portable?

G

Gibby Koldenhof

Hiya,

Quick question:

/* test.c, compile with gcc -ansi -pedantic -Wall test.c -o test */

int foo(int a, int b, int c, int d) { return 0; }
int bar(int a, int b, int c, int d) { return 1; }

int main(void)
{
int a = 1;

a = (a ? foo : bar)(0, 1, 2, 3);

return a;
}

Looks conforming to me [the (a ? foo : bar)] ... right?

Cheers,
Gibby Koldenhof
 
M

Martin Johansen

Gibby Koldenhof said:
Hiya,

Quick question:

/* test.c, compile with gcc -ansi -pedantic -Wall test.c -o test */

int foo(int a, int b, int c, int d) { return 0; }
int bar(int a, int b, int c, int d) { return 1; }

int main(void)
{
int a = 1;

a = (a ? foo : bar)(0, 1, 2, 3);

return a;
}

Looks conforming to me [the (a ? foo : bar)] ... right?

Cheers,
Gibby Koldenhof

Yes, I think this is OK, C is beautifull.

I know this is not:

(a ? foo : bar) = NULL;
 
P

Peter Nilsson

Gibby said:
/* test.c, compile with gcc -ansi -pedantic -Wall test.c -o test */

int foo(int a, int b, int c, int d) { return 0; }
int bar(int a, int b, int c, int d) { return 1; }

int main(void)
{
int a = 1;

a = (a ? foo : bar)(0, 1, 2, 3);

return a;
}

Looks conforming to me [the (a ? foo : bar)] ... right?

It's 'conforming', but that doesn't mean much! More importantly,
it is 'strictly conforming'. Although, without any output, this
is often true of programs which would otherwise only be
'conforming'.

But in answer to your real question, you can portably use
(compatible) function pointers as operands to the conditional
operator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,179
Latest member
pkhumanis73
Top