473,396 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why can't a defined symbol be part of a typedef?

Hi all.

I'm revising part of a large body of code that runs on a variety of
very different targets, and one of the things I'd like to do is move
away from certain "#define'd types" in favor of typedefs. This, mainly
because occasionally a user forgets that these are pointer "types",
and declares one or more variable on the same line, leading to errors.

E.g. right now I have this, which works fine, with the caveat that
only a single variable should be declared per line (because it's a
pointer variable):

typedef struct ecb OStypeEcb;
#define OStypeEcbP OStypeEcb OSLOC_ECB *

used thusly:

OStypeEcbP myEcbP;

and not

OStypeEcbP myEcbP, yourEcbP; // bad!

What is OSLOC_ECB, you ask? Well, it's a bank qualifier for certain
targets (e.g. Microchip PIC16's, where OSLOC_ECB might be nothing,
bank1, bank2 or bank3). The user can

#define OSLOC_ECB

or

#define OSLOC_ECB bank1

etc. to indicate where the objects (the ecb's) being pointed to are
located. So far, so good.

Naturally, I would prefer to have this:

typedef struct ecb OStypeEcb;
typedef OStypeEcb OSLOC_ECB * OStypeEcbP;

But it fails to compile, and I don't really understand why. OSLOC_ECB
is defined long before the typedef is processed by the compiler, yet
preprocessor output (CodeWarrior, in this case) of that line (for
OSLOC_ECB defined to be blank/nothing) reveals

typedef OStypeEcb OSLOC_ECB * OStypeEcbP;

instead of

typedef OStypeEcb * OStypeEcbP;

which is what I would have expected.

Can someone explain to me why defined symbols are not converted inside
the typedef, and if there's a way around this?

Thanks,

--Andrew
aek at pumpkininc dot com
Nov 14 '05 #1
5 1360

On Tue, 20 Jul 2004, aekalman wrote:

#define OSLOC_ECB typedef struct ecb OStypeEcb;
typedef OStypeEcb OSLOC_ECB * OStypeEcbP;

But it fails to compile, and I don't really understand why. OSLOC_ECB
is defined long before the typedef is processed by the compiler, yet
preprocessor output (CodeWarrior, in this case) of that line (for
OSLOC_ECB defined to be blank/nothing) reveals

typedef OStypeEcb OSLOC_ECB * OStypeEcbP;


Try this sample program in CodeWarrior. The code you're
describing is perfectly correct, so if it really is as you
describe, then CodeWarrior must have a bug. But that seems
unlikely to me...

#define FOO

typedef int FOO *bar;

int main()
{
bar baz;
baz = (int*)0;
}

If that code doesn't compile, then your compiler is broken.
On the other hand, are you absolutely *sure* you didn't have
the equivalent of

typedef int FOO *bar;

#define FOO

int main()
{
bar baz;
baz = (int*)0;
}

in your real code? Because that /isn't/ valid C, even though

#define bar int FOO *
#define FOO

is.

HTH,
-Arthur

Nov 14 '05 #2
aekalman wrote:
Hi all.

I'm revising part of a large body of code that runs on a variety of
very different targets, and one of the things I'd like to do is move
away from certain "#define'd types" in favor of typedefs. This, mainly
because occasionally a user forgets that these are pointer "types",
and declares one or more variable on the same line, leading to errors.

E.g. right now I have this, which works fine, with the caveat that
only a single variable should be declared per line (because it's a
pointer variable):

typedef struct ecb OStypeEcb;
#define OStypeEcbP OStypeEcb OSLOC_ECB *

used thusly:

OStypeEcbP myEcbP;

and not

OStypeEcbP myEcbP, yourEcbP; // bad!

What is OSLOC_ECB, you ask? Well, it's a bank qualifier for certain
targets (e.g. Microchip PIC16's, where OSLOC_ECB might be nothing,
bank1, bank2 or bank3). The user can

#define OSLOC_ECB

or

#define OSLOC_ECB bank1

etc. to indicate where the objects (the ecb's) being pointed to are
located. So far, so good. [...]


Not as far as I can see. With the latter definition
of OSLOC_ECB your sample usage goes

OStypeEcbP myEcbP;
-> OStypeEcb OSLOC_ECB * myEcbP;
-> OStypeEcb bank1 * myEcbP;

.... which looks like nonsense, unless `bank1' is itself
#define'd to something, or unless it's a special beyond-
the-C-language extension recognized by the compiler at
hand. If there's another #define lurking somewhere you
haven't shown it (or I've overlooked it). If `bank1' is
a non-C extension keyword, then you need to address your
question to the non-C compiler.

--
Er*********@sun.com

Nov 14 '05 #3
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi**********************************@unix45. andrew.cmu.edu>...
If that code doesn't compile, then your compiler is broken.
On the other hand, are you absolutely *sure* you didn't have
the equivalent of

typedef int FOO *bar;

#define FOO

int main()
{
bar baz;
baz = (int*)0;
}

in your real code? Because that /isn't/ valid C, even though

#define bar int FOO *
#define FOO

is.


You're spot-on. It turns out that I did indeed have the order mixed up
-- fixed that, and all is well.

Thank you very much ...

--Andrew
aek at pumpkininc dot com
Nov 14 '05 #4
Eric Sosman <Er*********@sun.com> wrote in message news:<40**************@sun.com>...

Not as far as I can see. With the latter definition
of OSLOC_ECB your sample usage goes

OStypeEcbP myEcbP;
-> OStypeEcb OSLOC_ECB * myEcbP;
-> OStypeEcb bank1 * myEcbP;

... which looks like nonsense, unless `bank1' is itself
#define'd to something, or unless it's a special beyond-
the-C-language extension recognized by the compiler at
hand. If there's another #define lurking somewhere you
haven't shown it (or I've overlooked it). If `bank1' is
a non-C extension keyword, then you need to address your
question to the non-C compiler.


I work with a multitude of C compilers for embedded targets, and
special memory qualifiers like bank1 / bank2 / bank3 (PIC16) and data
/ idata / xdata (8051) are de rigeur for processors with banked memory
schemes. My challenge is that some of the other targets (e.g. x86,
MSP430) have very nice linear memory spaces, and don't need such
qualifiers, yet a single code base must serve all targets and
compilers. So the preprocessor gets used a lot in my code ...

I apologize if this was off-topic for comp.lang.c. I approached it
from a perspective of general interest w/regard to the C preprocessor.

--Andrew
aek at pumpkin inc dot com
Nov 14 '05 #5
aekalman wrote:
Eric Sosman <Er*********@sun.com> wrote in message news:<40**************@sun.com>...
Not as far as I can see. With the latter definition
of OSLOC_ECB your sample usage goes

OStypeEcbP myEcbP;
-> OStypeEcb OSLOC_ECB * myEcbP;
-> OStypeEcb bank1 * myEcbP;

... which looks like nonsense, unless `bank1' is itself
#define'd to something, or unless it's a special beyond-
the-C-language extension recognized by the compiler at
hand. If there's another #define lurking somewhere you
haven't shown it (or I've overlooked it). If `bank1' is
a non-C extension keyword, then you need to address your
question to the non-C compiler.

I work with a multitude of C compilers for embedded targets, and
special memory qualifiers like bank1 / bank2 / bank3 (PIC16) and data
/ idata / xdata (8051) are de rigeur for processors with banked memory
schemes.


That's pretty much what I'd guessed. But since such
qualifiers are not part of the C language, but part of some
kind of "C with extensions" language, the question of whether
they can be part of a "type" (rather than just part of the
declaration of an identifier) is unanswerable in C, and is
entirely up to the vagaries of the compiler that supports
the extensions.

My challenge is that some of the other targets (e.g. x86,
MSP430) have very nice linear memory spaces, and don't need such
qualifiers, yet a single code base must serve all targets and
compilers. So the preprocessor gets used a lot in my code ...
Understandable. Sometimes you can segregate a lot of the
platform details into a layer of "pluggable" functions, but
sometimes they poke through to the upper levels and need to
be dealt with.
I apologize if this was off-topic for comp.lang.c. I approached it
from a perspective of general interest w/regard to the C preprocessor.


One rather pedantic difficulty is that the C preprocessor
operates on tokens ("preprocessing tokens," actually) rather
than on text. Nowhere is it required that these tokens have
intelligible textual representations; it is legitimate for the
tokenization process to destroy information or to add information
that isn't readily "textualized." And the upshot of all this is
that there's no requirement that you be able to see the source
after preprocessing.

However, many or even most compilers *do* have some way of
generating a text representation of the preprocessed program,
and I still don't understand why the output yours produced
suggests that the preprocessor ignored a defined macro. It's
certainly un-C-like to do so, and may indicate a compiler bug.
If so, you need help from experts in that particular compiler,
not help with the C language itself. Good luck!

--
Er*********@sun.com

Nov 14 '05 #6

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

Similar topics

14
by: Reply Via News Group Please | last post by:
Folks, I'm new to CSS and realise that by posting in the html newsgroup, I might upset one or two readers by raising a CSS question in an html newsgroup however my ISP only has one small CSS...
2
by: Peter Smithson | last post by:
Hi, I'm very new to C++ so bear with me. typedef list<string> jcllist; // list of strings typedef map<string, jcllist> proglist; // associative array of lists static proglist ProgList; I...
3
by: Dave | last post by:
Hello all, Please consider this code: #ifndef FOO_INCLUDED #define FOO_INCLUDED // File: foo.h class foo {
7
by: Santa Claus | last post by:
I have the following problem: I would like for a piece of code to be compiled only if a certain macro has been defined AND has some specific value. Let me illustrate: #ifdef SYMBOL f() ;...
6
by: Mark Jerde | last post by:
I need to call a Win32 DLL. The API supports both Windows and Linux, but I'm only concerned with Windows. I'd like advice whether I can use C# or whether I'll have to dust off my old C++ books. ...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
2
by: curious2007 | last post by:
During the linking I get the following: 1>Linking... 1>main.obj : error LNK2005: "double __cdecl sigma(class curious2007::pair<double,double> const &)" (?sigma@@YANABV?$pair@NN@curious2007@@@Z)...
7
by: Rob | last post by:
This actually compiles and works but it doesn't seem like the best code, so I was wondering is there another way to do this? template <typename Tvector<T>* addDepth(T) { return new vector<T>;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.