Simple but interesting issue

M

Mike

Hi All,

What is the differece between following code:

#define sbyte m_sbyte
typedef signed char m_sbyte;

and

#typedef signed char sbyte



Actually the first two lines of code solves the decleration
confliction issue. When sbyte is decleared as "unsigned char", the
first two lines of code will allow you to use the sbytes and signed
char. Anyone konws why?
 
C

Chris Dollin

Mike said:
Hi All,

What is the differece between following code:

#define sbyte m_sbyte
typedef signed char m_sbyte;

and

#typedef signed char sbyte

The second is illegal, as there's no #typedef preprocessor
directive. Did you mean

typedef signed char sbyte;

?
Actually the first two lines of code solves the decleration
confliction issue.

What declaration conflict issue?
When sbyte is decleared as "unsigned char", the
first two lines of code will allow you to use the sbytes and signed
char. Anyone konws why?

I've no idea what you mean. Can you be more specific?
 
M

mark_bluemel

Hi All,

What is the differece between following code:

#define sbyte m_sbyte
Tell the precompiler to replace the string "sbyte" with "m_sbyte" in
the source code.
typedef signed char m_sbyte;
Tell the compiler that "m_sbyte" means "signed char".
and

#typedef signed char sbyte

(pre-)compiler error - "#typedef" is not recognised.

Either you meant :-
"typedef signed char sbyte"
which tells the compiler that "sbyte" means "signed char"

or you meant
"#define signed char sbyte"
which tells the precompiler to replace all instances of "signed" with
"char sbyte".
Actually the first two lines of code solves the decleration
confliction issue. When sbyte is decleared as "unsigned char", the
first two lines of code will allow you to use the sbytes and signed
char. Anyone konws why?

I don't know what you are trying to say.

How about cutting and pasting some real code, preferably a small but
complete example, and showing us what problem you are encountering?
 
M

Mike

The second is illegal, as there's no #typedef preprocessor
directive. Did you mean

typedef signed char sbyte;

?


What declaration conflict issue?


I've no idea what you mean. Can you be more specific?

Sorry about the confusion. Here is the case:

In module A, sbyte is declared as unsigned char;
In module B, sbyte is declared as singed char;

When both modules are compiled together, there will be declaration
confliction. Now if you use the first two lines of code in module B.
Everything is OK, no need to change any code in Module A and B.
 
C

Chris Dollin

Mike wrote:
Sorry about the confusion. Here is the case:

In module A, sbyte is declared as unsigned char;
In module B, sbyte is declared as singed char;

Well, I'd say that was a mistake. Especially giving `unsigned
char` the abbreviation `sbyte`. I'll be giving /that/ programmer
no biscuit.
When both modules are compiled together, there will be declaration
confliction.

Not unless those types are declared in the /headers/ of the
compilation units. (I assume they're not being compiled as
a single unit -- that way madness lies.)
Now if you use the first two lines of code in module B.
Everything is OK, no need to change any code in Module A and B.

Even better is to fix the typedefs, in any one of several ways:

* if the typedefs aren't needed in the headers, take them out.

* if the typedefs are supposed to be the same thing, abstract
them out into a common header file.

* if the typedefs are supposed to be different things, apply
whatever your C-doesn't-have-namespaces-but module naming
convention rules are.
 
E

Eric Sosman

Mike wrote On 03/16/07 10:46,:
[...]

In module A, sbyte is declared as unsigned char;
In module B, sbyte is declared as singed char;

<fx: sniff, sniff> Is something burning?

(Sorry, but the idea of a singed char is a matchless
inadvertent pun. I couldn't resist giving it some light,
even if I now catch heat for topicality abuse. Let the
flame wars begin, so we may smoke out the humor-impaired!)
 
R

Richard Tobin

[/QUOTE]
Sorry about the confusion. Here is the case:

In module A, sbyte is declared as unsigned char;
In module B, sbyte is declared as singed char;

When both modules are compiled together, there will be declaration
confliction. Now if you use the first two lines of code in module B.
Everything is OK, no need to change any code in Module A and B.

Presumably the #define and typedef appear in B after the inclusion of
the header file for A. The result is that B doesn't really use sbyte
at all, because all occurences of it after the #define will be replaced
with m_sbyte.

This sort of hack is quite common when dealing with existing libraries
that have name clashes, but you have to be very careful - should *all*
the uses of sbyte in module B be the signed version? - and where
possibly you should try to resolve the underlying name clash. Many
other languages have some kind of namespace mechanism to solve this.

-- Richard
 
F

Flash Gordon

Eric Sosman wrote, On 16/03/07 15:08:
Mike wrote On 03/16/07 10:46,:
[...]

In module A, sbyte is declared as unsigned char;
In module B, sbyte is declared as singed char;

<fx: sniff, sniff> Is something burning?

(Sorry, but the idea of a singed char is a matchless
inadvertent pun. I couldn't resist giving it some light,
even if I now catch heat for topicality abuse. Let the
flame wars begin, so we may smoke out the humor-impaired!)

It's probably the flame wars that singed the char in the first place!
 
C

CBFalconer

Flash said:
Eric Sosman wrote, On 16/03/07 15:08:
Mike wrote On 03/16/07 10:46,:
[...]

In module A, sbyte is declared as unsigned char;
In module B, sbyte is declared as singed char;

<fx: sniff, sniff> Is something burning?

(Sorry, but the idea of a singed char is a matchless
inadvertent pun. I couldn't resist giving it some light,
even if I now catch heat for topicality abuse. Let the
flame wars begin, so we may smoke out the humor-impaired!)

It's probably the flame wars that singed the char in the first place!

I believe singed chars have to be displayed in a sans-serif font.
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top