strange function syntax

N

Neil Kurzman

GS said:
I am doing my first real embedded programming project and the supplied
device libraries have a function definition that looks like this:

void FCN(void) = { INTDEF, INTABS, (unsigned short) PTRDEF};

where
INTDEF = some #defined integer
INTABS = some absolute integer (e.g. 2)
PTRDEF = some #defined pointer

The reason I ask is that my program is getting hung up (i.e. program
execution freezes without any exceptions/errors) on a call to this type of
function, but only when I compile with a certain amount of optimization
(everything works fine w/o optimization). This could be a compiler problem
I imagine but I'd like to know what the above statement does.

Thanks for your help.

This is a wild guess. But is the the code trying the generate a function
address? Or call a function by its address?
 
J

Jeremy Yallop

Keith said:
[...]
The fact that FCN is in all-caps makes me suspect that it's defined
as a macro. If FCN is not a macro, the code fragment is not legal
C. If FCN is a macro, it might conceivably be legal C, but I can't
think of a definition that would it legal.

#define FCN(x) *p; int a[]

Jeremy.
 
T

Thomas stegen

Olavi said:
Though, this perfectly normal in comp.lang.postscript is.

It is not normal here. And that is what counts. If
I ever need to discuss postscript I'll top post if
required to.

What is the bid deal?
 
C

Case -

Arthur said:
I have seen something similar


Not similar to GS's code, since at least the IOCCC entry parses
correctly. The syntax

void foo(void) = { ... }

is quite simply invalid, whereas the syntax

int foo[] = { ... }

is a standard way to define an array.

Yes I know.
as one of the winning
entries of the OCCC (Obfuscated C Code Contest) somewhere
in the 90s. It looked something like:

int main[] = { 0xeefa, 0xed48, 0x45ed, ....... };

Is this valid C? But is compiled and ran on a PDP11-70
printing "Hello world!" I believe.


It is not conforming C on a hosted implementation, according to
N869, because the declaration of 'main' as an array with external
linkage precludes the definition of 'main' as a function with
external linkage. Thus we have no 'main' function in a hosted
environment, which yields UB.
In a non-hosted environment, anything goes.
If you make it 'static int main[] = ...', then I think it is
valid C (assuming 'int main()' is defined in a different TU).

That this IOCCC code worked on a PDP-11 compiler is not surprising;
it might even work on a modern DOS-based compiler (with appropriate
adjustments to the machine-code part of the program). But since in
standard C, data is never code, this is definitely *not* guaranteed
to do anything in particular.

HTH,

The only 'help' I needed was related to C standard conformance.
Thanks for the explanation!
 
C

Case -

Jeremy said:
Keith said:
GS wrote:

#define INTDEF 0x08F8
#define PTRDEF 0xFFFFFA1F
void FCN(void) = {INTDEF, 2, (unsigned short) PTRDEF};
[...]

The fact that FCN is in all-caps makes me suspect that it's defined
as a macro. If FCN is not a macro, the code fragment is not legal
C. If FCN is a macro, it might conceivably be legal C, but I can't
think of a definition that would it legal.


#define FCN(x) *p; int a[]

I knew it. Where did you find this?
 
E

Eric Sosman

Peter said:
Of course it's a compiler-specific C-with-bells extension. Of course
it's not _strictly_ conforming, but it is 'conforming' under the
definition of the standards.

True, in the sense that

public static void main(String[] args) { }

is "conforming:" The compiler can attach any meaning it likes
to a non-C construct, and can document that extension and
perhaps even make it useful. However, a conforming compiler
must also issue a diagnostic; the O.P. doesn't specifically
tell us that there's no diagnostic, but he never mentioned
any and wrote that "this syntax compiles." If it compiles
without a diagnostic, I believe the compiler is non-conforming.
 
A

Arthur J. O'Dwyer

Jeremy said:
Keith said:
GS wrote:

#define INTDEF 0x08F8
#define PTRDEF 0xFFFFFA1F
void FCN(void) = {INTDEF, 2, (unsigned short) PTRDEF};
The fact that FCN is in all-caps makes me suspect that it's defined
as a macro. If FCN is not a macro, the code fragment is not legal
C. If FCN is a macro, it might conceivably be legal C, but I can't
think of a definition that would it legal.

#define FCN(x) *p; int a[]

I knew it. Where did you find this?

He wrote it. It's not too difficult. Expanding this macro
in GS's code gives

void *p; int a[] = {INTDEF, 2, (unsigned short) PTRDEF};

which is obviously valid C code; it defines a (void *) object called
'p' and an array of three ints called 'a'.
It doesn't do anything useful, though --- all it does is make the
code compile. In particular, it's still a syntax error to try to
"call" FCN using the FCN() syntax.

-Arthur
 

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
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top