474,034 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

defines and structure instantiation

Suppose the following:

typedef void (*funcptrs)(voi d);

typedef struct {
unsigned int *in;
unsigned int *out;
unsigned int *overrun;
funcptrs myptrs[1]; /* >=1 for ansi */
} yada;

Is it possible to write a #define in which I can vary the storage array
size? What I mean is suppose you had:

mystruc(10, name)
mystruc(20, name)

and a #define mystruc(A,B) ...

The A would set the size of the myptrs array and the B would be the name of
the declared structure. Perhaps a union might allow the define to create
the appropriate size array? The issues is that for the hardware I'm
targeting the unsigned int *s will be 2 bytes (point to RAM) and the void
(*)(void) will be 3 bytes (point to code space in flash). If they were
both two bytes it would be easier (at least for me!). Any good ideas on
this?

Thanks

PS: I cross posted this to the moderated forum inadvertantly.. .sorry!
May 27 '06 #1
5 5259
Rich wrote:
Suppose the following:

typedef void (*funcptrs)(voi d);

typedef struct {
unsigned int *in;
unsigned int *out;
unsigned int *overrun;
funcptrs myptrs[1]; /* >=1 for ansi */
} yada;

Is it possible to write a #define in which I can vary the storage array
size? What I mean is suppose you had:

mystruc(10, name)
mystruc(20, name)

and a #define mystruc(A,B) ...

The A would set the size of the myptrs array and the B would be the name of
the declared structure.


#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B

mystruc(10, test);

Robert Gamble

May 27 '06 #2
Robert Gamble wrote:

#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B

mystruc(10, test);


Thanks! I'm still having some difficulty:

I have this in a header file:

#define mystruc(A,B) struct { \
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A+1]; \
} B = {(unsigned int *)&B.funcptrs[0],(unsigned int *)&B.funcptrs[0]
(unsigned int *)&B.funcptrs[A]}

This is in code:
mystruc(10, myq);

Now, when I try to use myq as in the following:
myfunc((unsigne d int *)myq);

where myfunc is defined as:
myfunc(unsigned int *qu){
Qtype *q = (Qtype *)qu;
q->remove = q->insert = (unsigned int *)&q->mem[0];
}
I receive errors about myq being unable to cast from an anonymous stuct to
unsinged int. I'm trying to create unsigned int pointers that store
pointers into RAM space where 3 byte function pointers are stored.

Thanks again.

May 27 '06 #3
Rich wrote:
Robert Gamble wrote:

#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B

mystruc(10, test);
Thanks! I'm still having some difficulty:

I have this in a header file:

#define mystruc(A,B) struct { \
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A+1]; \
} B = {(unsigned int *)&B.funcptrs[0],(unsigned int *)&B.funcptrs[0]
(unsigned int *)&B.funcptrs[A]}


I'm not sure what you are trying to do in this last line, it doesn't
make much sense to me. It looks like you are trying to make the
initializer part of the macro but the structure doesn't have a member
named funcptrs and even if it did the initializer would be invalid
because it tries to initialize part of itself to parts that have not
been initialized yet. I would do the initialization seperately if I
were you.
This is in code:
mystruc(10, myq);

Now, when I try to use myq as in the following:
myfunc((unsigne d int *)myq);
You are casting the value of the structure to a pointer which doesn't
make any sense, you probably want to cast the address of the structure:

myfunc((unsigne d int *)&myq);
where myfunc is defined as:
myfunc(unsigned int *qu){
Qtype *q = (Qtype *)qu;
q->remove = q->insert = (unsigned int *)&q->mem[0];
}
I receive errors about myq being unable to cast from an anonymous stuct to
unsinged int.


I don't know what a Qtype is so that doesn't mean much to me. Instead
of summerizing the errors you get, just copy and paste the error
message itself. Making the change above should fix the error you
describe.

Robert Gamble

May 27 '06 #4
Robert Gamble wrote:
Rich wrote:
Robert Gamble wrote:

#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B

mystruc(10, test);


Thanks! I'm still having some difficulty:

I have this in a header file:

#define mystruc(A,B) struct { \
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A+1]; \
} B = {(unsigned int *)&B.funcptrs[0],(unsigned int *)&B.funcptrs[0]
(unsigned int *)&B.funcptrs[A]}


I'm not sure what you are trying to do in this last line, it doesn't
make much sense to me. It looks like you are trying to make the
initializer part of the macro but the structure doesn't have a member
named funcptrs and even if it did the initializer would be invalid
because it tries to initialize part of itself to parts that have not
been initialized yet. I would do the initialization seperately if I
were you.


Ignore what I said in the second sentence. The reason it would not be
valid is because the address of the array elements is not available
during the initialization. Something like this should work:

#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B; B.in = B.out = (unsigned int *)&B.myptrs[0]; \
B.overrun = (unsigned int *)&B.myptrs[A];

Make sure you copy&paste your code instead of re-typing it as you
obviously did here (you were missing a comma in the initializer and the
member name is myptrs, not funcptrs which your compiler should have
caught). I assume that you can safely convert a function pointer to a
pointer to unsigned int on your platform, the ability to safely do this
is not guaranteed by the Standard.

Robert Gamble

May 27 '06 #5
Assuming I got all the ">>>"s right:
Robert Gamble wrote:

#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B

mystruc(1 0, test);

Note that this creates an anonymous type, so the only thing
you can do with the object is work directly with it. (In
particular you cannot create a pointer that points to this
type, because you have no associated name.)

You can, however, apply the "struct hack" (or C99's flexible
arary member technique) with reasonable hope for portability.
Rich wrote:

Thanks! I'm still having some difficulty:

I have this in a header file:

#define mystruc(A,B) struct { \
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A+1]; \
} B = {(unsigned int *)&B.funcptrs[0],(unsigned int *)&B.funcptrs[0]
(unsigned int *)&B.funcptrs[A]}

This is ... suspicious at best.
Robert Gamble wrote:
I'm not sure what you are trying to do in this last line, it doesn't
make much sense to me. It looks like you are trying to make the
initializer part of the macro but the structure doesn't have a member
named funcptrs

This is one of the big problems ...
and even if it did the initializer would be invalid because it tries
to initialize part of itself to parts that have not been initialized
yet.
but this is not: &B.funcptrs[0] would be valid (as an "address
value" for an initializer, which is not a constant but is a valid
initializer even for objects with static duration) provided that
"B" itself has static duration. (If "B" has automatic duration,
this is only valid in C99.)
I would do the initialization seperately if I were you.


That should only help if B has automatic duration and the compiler
only handles C89 (or C90 or C94) instead of C99.

A bigger problem, to me anyway, is the use of "unsigned int *".
"funcptrs" (which was snipped) is an alias for "void (*)(void)",
i.e., pointer to function (of no arguments) returning void.
If B is an object (of unname-able type) containing a member
of type "array N of (void (*)(void))", then &B.member[i] has
type "pointer to (void (*)(void))" or "void (**)(void)":

#define DECLARE(n, typename, objectname) \
struct typename { \
void (**in)(void); \
void (**out)(void); \
void (**overrun)(voi d); \
void (*funcs[(n)+1])(void); \
} objectname = { \
&objectname.fun cs[0], \
&objectname.fun cs[0], \
&objectname.fun cs[n] \
}

which now needs no casts. Of course you still need to fill
in the actual function pointer values.

In article <11************ *********@j33g2 000cwa.googlegr oups.com>,
Robert Gamble <rg*******@gmai l.com> wrote:Ignore what I said in the second sentence. The reason it would not be
valid is because the address of the array elements is not available
during the initialization.
Only true for objects with automatic duration, in which case the
entire brace-enclosed contents are not valid initializers for an
object with static duration -- but we just said that the object has
automatic duration! In C99, the brace-enclosed list becomes a
compound literal, which has a lot of latitude. (Only compound
literals that are outside functions require constant-expressions,
and if this declaration were outside a function, the object would
have static duration.)

[snippage]
Make sure you copy&paste your code instead of re-typing it as you
obviously did here (you were missing a comma in the initializer and the
member name is myptrs, not funcptrs which your compiler should have
caught). I assume that you can safely convert a function pointer to a
pointer to unsigned int on your platform, the ability to safely do this
is not guaranteed by the Standard.


I left all this in for emphasis, as it were. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jun 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2930
by: Jean-Claude Gervais | last post by:
Hi, I'm trying to write a template class that knows its own name. Specifically, I want it to behave this way - Say I instantiate a template class like this: my_vector<int> foo;
12
2643
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
13
1735
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." here: http://jibbering.com/faq/faq_notes/closures.html
11
2368
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of policies, I want to customize the structure of a class. The idea is to investigate the use of several data structures. One option would be the use of the boost dynamic bitset. Another would be the use of the std::vector. I obtained some code that...
3
4020
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF, from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
2
1890
by: WittyGuy | last post by:
Hi My class looks something like this: class Base { public: Base () {} ~Base () {} // No virtual dtor in the base class private: };
2
3691
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
4
2502
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
8
1817
by: =?ISO-8859-1?Q?m=E9choui?= | last post by:
Problem: - You have tree structure (XML-like) that you don't want to create 100% in memory, because it just takes too long (for instance, you need a http request to request the information from a slow distant site). - But you want to be able to request data from it, such has "give me all nodes that are under a "//foo/bar" tree, and have a child with an "baz" attribute of value "zzz". Question :
0
10520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
12112
Oralloy
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...
1
11968
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
11120
tracyyun
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...
1
8677
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7844
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();...
0
6803
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5387
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
2
4924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.