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

function addresses at build time...?

P: n/a
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.
Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Nov 13 '05 #2

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

Nov 13 '05 #3

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

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #4

P: n/a
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:fq********************************@4ax.com
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?


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.
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Nov 13 '05 #5

P: n/a
Jack Klein wrote:

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

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Nov 13 '05 #6

P: n/a
Charles Richmond wrote:
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.

--
pete
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.