On 04 Sep 2003 01:15:52 GMT,
dc********@aol.com (DCSudolcan) wrote in
comp.lang.c:
I know that a program can create and properly initialize an array of pointers
to functions at build time, but can something like the following be done at
build time?
void foo(void);
unsigned char myArray[]={
(unsigned char) (foo&0xFF),
(unsigned char) ((foo&0xFF00)>>8),
(unsigned char) ((foo&0xFF0000)>>16),
(unsigned char) ((foo&0xFF000000)>>24),
0x00, // other data...
0x01, // other data...
0x02, // other data...
0x03, // other data...
0x04 // other data...
};
My goal is to be able to initialize an array at build time with a variety of
instances of a variety of differently sized data values, including pointers to
functions encoded as arrays of bytes. Is it possible to do this (and get valid
results)? Or, is there no way for compilers to resolve this type of problem.
Confuzed...
Dave.
If what you mean by "build time" is during translation (C doesn't
define the term "build"), then no, you can't.
All values in an initializer list for an object with static storage
duration must meet the requirements for constant expressions. The
only thing you can do with a pointer to an object with static duration
in a constant expression is add or subtract an integral constant.
And you can't do shifts or bit-wise logical operations on pointers of
any type at any time.
During execution, there is the possibility of casting an object
pointer to an integer type, although no guarantee that it is possible
on all implementations. But there is no defined conversion at all in
C from a pointer to function to any type other than pointer a function
with a different signature.
There is no defined conversion from pointer to function to any integer
type, and there is no defined conversion from pointer to function to
pointer to any object type.
As K&R said, 25 years ago, there are two things you can do with a
function, call it or take its address. What they didn't add is that
the only thing you can do with the address of a function is use it to
call the function.
Trying to do anything else at all with the address of a function is,
was, and most likely always be, totally undefined behavior.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
ftp://snurse-l.org/pub/acllc-c++/faq