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&0xFF00000 0)>>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. 6 2069
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&0xFF00000 0)>>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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
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&0xFF00000 0)>>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.l earn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html
Other sites: http://www.josuttis.com -- C++ STL Library book
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
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:fq******** *************** *********@4ax.c om 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
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 |
+----------------------------------------------------------------+
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: BadOmen |
last post by:
I have made an array in a function in a Class now I need to get to that
Array from an other function in an other Class, How do I do that?
The Array has no fixed size as it is dependent on how much a user has put in
a txt file.
The Class with the Array is an object, objInternetClass and I want to reach
the Array by doing something like
This is the MSAgentClass:
|
by: anonymous |
last post by:
Thanks your reply.
The article I read is from
www.hakin9.org/en/attachments/stackoverflow_en.pdf.
And you're right. I don't know it very clearly. And that's why I want
to understand it; for it's useful to help me to solve some basic
problem which I may not perceive before.
I appreciate your help,
sincerely.
|
by: Nicole |
last post by:
Is there a way to explicitly define the address pointed to by a function
pointer without using the address-of operator? Such as:
void (*ptrFunction)(void) = NULL;
void funcA(void); //Address of this function is 0x0040010A
int main(void) {
ptrFunction = &funcA; //this works.
ptrFunction = 0x0040010A; //this doesn't, and I want it to.
|
by: sck10 |
last post by:
Hello,
I have a list of email addresses that I need to send email to from the
website. I am trying to use the "Split" function to get all the To's and
then use the uBound function for the For-Loop limit:
I am trying to convert the following from vb to c#:
Dim SplitCatcher As Object
SplitCatcher = Split(To, ",")
|
by: Cuthbert |
last post by:
After compiling the source code with gcc v.4.1.1, I got a warning
message:
"/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the
'gets' function is dangerous and should not be used."
Could anybody tell me why gets() function is dangerous??
Thank you very much.
Cuthbert
| |
by: Rico |
last post by:
Hello,
I have a generic process that logs errors from different sources. When I
call this code, I'd like to also submit the name of the function or sub that
is raising the error without having to manually type this in for each
instance. For instance, (air code to follow)
Public Sub LogMyError( errNumber as int, errDescription as string,
SubFuncName as string)
|
by: Peter Olcott |
last post by:
I want to make a generic interface between a scripting language and native code,
the native code and the interpreter will both be written in C++. The interpreter
will probably be implemented as a subset of C/C++, thus will have the same
syntax as C/C++.
Somehow the interpreted code must be able to store generic function pointers
because there is no way for the interpreter to know every possible function
signature in advance. I was...
|
by: Spartacus |
last post by:
Hello,
I know there is lots of information out there and I've taken a look, but
I want to learn how to do this myself. And I'm a beginner here and some
of it I don't understand (so no flames, RTFM or GFGI please ;)
Here's my function:
function send_mail($to_name, $to_email, $from_name, $from_email,
$subject, $message)
|
by: shailesh |
last post by:
Hi,
I am facing a peculiar problem. socket.gethostbyaddr is not working
fine on my MAC for ip addresses on the LAN. The LAN happens to consist
of linux and windows machines and this is the only one MAC on the
LAN.
Please see the example below. I am getting error: socket.herror: (1,
'Unknown host')
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |