function addresses at build time...?

D

DCSudolcan

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.
 
J

Jack Klein

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
 
T

Thomas Matthews

DCSudolcan said:
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.

C is a typed language. If you want arrays of objects, they must be
homegenous. You can have an array of function pointers and an array
of doubles. You can't have an array that has function pointers
interleaved with doubles. You can have an array of structures where
each structure has a pointer and a double.

You _can_ however, have an array of anything and cast the values
to the type of the array. The outcome is compiler and platform
specific and not portable. A common example is convert pointers
to integers. Some platforms treat them the same. Other platforms
can have a Segment:Offset format which doesn't fit into an integer.

The choice is yours.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
T

Tom Zych

DCSudolcan said:
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.

You can't do it at compile (not "build") time, but you can do it
first thing at run time, which should probably suffice.

C arrays are homogeneous, you can't mix types. One way to do what
you're trying to do would be to have an array of pointers to a
struct like this:

struct bitbucket {
int type;
void *data;
};

Then just use memcpy() to get the data in and out of the array.
 
D

Dave Sudolcan

Thanks to all for your answers. Now I know why I couldn't do what I was
trying to do,
and now I know several other ways that I can try to solve my original
problem. In
hindsight, I'm disappointed that the compiler can put function addresses
into an array,
somehow, but can't allow casting a 32-bit pointer as a 32-bit unsigned
int and operating
on it in the static sort of fashion I'd hoped was possible. Oh-well,
there are always
so many different ways to solve problems, I'll just solve it
differently.

Thanks again for your answers.
Dave.
 
C

Charles Richmond

Jack said:
[snip...] [snip...] [snip...]

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.
You can pass a pointer to a function...as an argument to another
function. And you can return a pointer to a function from another
function. You can assign the pointer to a function to any variable
of the proper "pointer to a function" type. You can also print out
the value of a pointer to a function with "printf()" using the %p
format descriptor.
 
P

pete

Charles said:
You can also print out
the value of a pointer to a function with "printf()" using the %p
format descriptor.

%p is for printing the address of type pointer to void.
Functions aren't objects and the only allowances for casting
functions to void, are in the J.5 common extensions,
which really aren't part of the standard language.
 

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,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top