473,386 Members | 1,766 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,386 software developers and data experts.

Interesting features of #define and typedef, correct me if I am wrong

Look at these two codes:

===================================
#define int_ptr int*
int_ptr a, b;
===================================

and

===================================
typedef int* int_ptr
int_ptr a, b;
===================================

In first example, only a is a pointer-to-an-integer but b is an
integer.

At second example, both of a, b are pointer-to-an-integer.

Do you guys concur with me?

Feb 1 '07 #1
13 1699
<fd*******@gmail.comwrote in message
Look at these two codes:

===================================
#define int_ptr int*
int_ptr a, b;
===================================

and

===================================
typedef int* int_ptr
int_ptr a, b;
===================================

In first example, only a is a pointer-to-an-integer but b is an
integer.

At second example, both of a, b are pointer-to-an-integer.

Do you guys concur with me?
That's one of the reasons you need typedef. The syntax for declaring
pointers is a bit unusual, though it makes sense in a formal grammary sort
of way.
Feb 1 '07 #2
Malcolm McLean wrote:
<fd*******@gmail.comwrote in message
>>Look at these two codes:

===================================
#define int_ptr int*
int_ptr a, b;
===================================

and

===================================
typedef int* int_ptr
int_ptr a, b;
===================================

In first example, only a is a pointer-to-an-integer but b is an
integer.

At second example, both of a, b are pointer-to-an-integer.

Do you guys concur with me?

That's one of the reasons you need typedef. The syntax for declaring
pointers is a bit unusual, though it makes sense in a formal grammary sort
of way.
It's also a good reason for not declaring more than one variable per line.

--
Ian Collins.
Feb 1 '07 #3

"Ian Collins" <ia******@hotmail.comwrote in message
news:52*************@mid.individual.net...
Malcolm McLean wrote:
><fd*******@gmail.comwrote in message
[OP]
>>
That's one of the reasons you need typedef. The syntax for declaring
pointers is a bit unusual, though it makes sense in a formal grammary
sort
of way.
It's also a good reason for not declaring more than one variable per line.
This post is typical of a variety that I don't really understand. It uses
the idioms of C to do things that are ill-advised. Why? Because it's
unclear and unnecessary. With preprocessor stuff and type definitions, the
world is a better place when whitespace and carriage return delimit the
statements liberally. What makes a #define statement interesting is the
content of what is being defined, not the statement itself. LS
Feb 1 '07 #4
fd*******@gmail.com wrote:
===================================
#define int_ptr int*
int_ptr a, b;
===================================

and

===================================
typedef int* int_ptr
int_ptr a, b;
===================================

In first example, only a is a pointer-to-an-integer but b is an
integer.

At second example, both of a, b are pointer-to-an-integer.
The base type is int. The '*' denotes the identifier as being a pointer to
that base type.

You're operating from the paradigm that the base type is pointer-to-int, which
it isn't - it's int.
Feb 2 '07 #5
fd*******@gmail.com said:
Look at these two codes:

===================================
#define int_ptr int*
int_ptr a, b;
===================================

and

===================================
typedef int* int_ptr
int_ptr a, b;
===================================

In first example, only a is a pointer-to-an-integer but b is an
integer.

At second example, both of a, b are pointer-to-an-integer.

Do you guys concur with me?
I don't understand why you think it's interesting.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 2 '07 #6
On Feb 2, 11:36 am, Richard Heathfield <r...@see.sig.invalidwrote:

<snip>
===================================
#define int_ptr int*
int_ptr a, b;
===================================
and
===================================
typedef int* int_ptr
int_ptr a, b;
===================================
In first example, only a is a pointer-to-an-integer but b is an
integer.
At second example, both of a, b are pointer-to-an-integer.
Do you guys concur with me?

I don't understand why you think it's interesting.
Is it true that typedef increases code size but #define does_NOT_,
since latter being the preprocessor.

Thanks,
Nishu

Feb 2 '07 #7
Nishu said:

<snip>
Is it true that typedef increases code size but #define does_NOT_,
since latter being the preprocessor.
It depends on what you mean by code size. You might conceivably mean the
source code, in which case of course they both increase it, just as any
language construct does, including whitespace.

But if you mean the object code, then no, it isn't true. For one thing,
typedefs needn't increase code size. For another, #defines definitely can.
Thirdly, #defines are not the preprocessor. They are merely expanded by the
preprocessor. "Expanded" should give you an extra clue here.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 2 '07 #8
"Nishu" <na**********@gmail.comwrites:
[...]
Is it true that typedef increases code size but #define does_NOT_,
since latter being the preprocessor.
No. A typedef merely creates an alias for an existing type. It's a
notational convenience. I can't think of any reason why it would
increase code size. If you can explain what you mean (increase code
size compared to what?), perhaps we can explain further.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 2 '07 #9
On Feb 2, 12:58 pm, Keith Thompson <k...@mib.orgwrote:
"Nishu" <naresh.at...@gmail.comwrites:

[...]
Is it true that typedef increases code size but #define does_NOT_,
since latter being the preprocessor.

No. A typedef merely creates an alias for an existing type. It's a
notational convenience. I can't think of any reason why it would
increase code size. If you can explain what you mean (increase code
size compared to what?), perhaps we can explain further.
By increase in code size, I meant increase in object code size (or the
library size which includes least symbols(in release mode)) by using
typedef instead of preprocessor.

I've a notion that Preprocessors were simply replaced by Preprocessor
before compiler/assembler generates the object code but for typedef
there might be some implicit conversions since it is not a
preprocessor.

Thanks,
Nishu

Feb 2 '07 #10
Nishu a écrit :
On Feb 2, 12:58 pm, Keith Thompson <k...@mib.orgwrote:
>>"Nishu" <naresh.at...@gmail.comwrites:

[...]

>>>Is it true that typedef increases code size but #define does_NOT_,
since latter being the preprocessor.

No. A typedef merely creates an alias for an existing type. It's a
notational convenience. I can't think of any reason why it would
increase code size. If you can explain what you mean (increase code
size compared to what?), perhaps we can explain further.


By increase in code size, I meant increase in object code size (or the
library size which includes least symbols(in release mode)) by using
typedef instead of preprocessor.

I've a notion that Preprocessors were simply replaced by Preprocessor
before compiler/assembler generates the object code but for typedef
there might be some implicit conversions since it is not a
preprocessor.

Thanks,
Nishu
You are wrong. The object code size will be the same, since the assembly
instructions for using an int pointer or a typedefed int pointer will be
exactly the same.

Of course, if your compiler generates DEBUG INFORMATION then it will
generate a record for the typedef. But many compilers (lcc-win32 for
instance) generate ALSO records for the #defines, noting if it is a
macro or just a replacement, etc. This will be highly specific to the
format of the debug information but I would guess it will be almost the
same.
Feb 2 '07 #11

"Nishu" <na**********@gmail.comwrote in message
>
>No. A typedef merely creates an alias for an existing type. It's a
notational convenience. I can't think of any reason why it would
increase code size. If you can explain what you mean (increase code
size compared to what?), perhaps we can explain further.

By increase in code size, I meant increase in object code size (or the
library size which includes least symbols(in release mode)) by using
typedef instead of preprocessor.

I've a notion that Preprocessors were simply replaced by Preprocessor
before compiler/assembler generates the object code but for typedef
there might be some implicit conversions since it is not a
preprocessor.
A compiler can do what it wants.
So it is conceivable that it might add an ASCII string representing the
typedef to some intermediate file, even the final executable. However
normally typedef would be resolved to the basic type by the front end, and
the object have the plain signature.
Feb 3 '07 #12

"Nishu" <na**********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
On Feb 2, 12:58 pm, Keith Thompson <k...@mib.orgwrote:
>"Nishu" <naresh.at...@gmail.comwrites:

[...]
Is it true that typedef increases code size but #define does_NOT_,
since latter being the preprocessor.

No. A typedef merely creates an alias for an existing type. It's a
notational convenience. I can't think of any reason why it would
increase code size. If you can explain what you mean (increase code
size compared to what?), perhaps we can explain further.

By increase in code size, I meant increase in object code size (or the
library size which includes least symbols(in release mode)) by using
typedef instead of preprocessor.

I've a notion that Preprocessors were simply replaced by Preprocessor
before compiler/assembler generates the object code but for typedef
there might be some implicit conversions since it is not a
preprocessor.
#define verbose damnit
I think it makes next to no sense to talk about code size with either a
defines or a typedef. You take all the room you need. LS
Feb 6 '07 #13
Lane Straatman wrote:
I think it makes next to no sense to talk about code size with either a
defines or a typedef. You take all the room you need. LS
Yes, and there is no reason why increased room would be needed either
way.

--
DPS
Feb 12 '07 #14

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

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
10
by: lallous | last post by:
Hello I noticed that when I use #define inside a namespace then this #define only works when prefixed with the namespace name. Is this behaviour standard or just compiler specific? I use...
9
by: vashwath | last post by:
Hi all, Recently I attended an interview in which the question "Is there any difference between "const T var" and "T const var"? was asked.I answered "NO"(I guessed it:-( ).Did I answered...
71
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef...
9
by: Mike | last post by:
Hi All, What is the differece between following code: #define sbyte m_sbyte typedef signed char m_sbyte; and #typedef signed char sbyte
3
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I...
5
by: suresh | last post by:
Hi, How to define a two dimensional array where each row is of type vector<map<string,int>>? My idea is, if "x" is such a variable, x is a vector where each cell of the vector is a...
6
by: dvanguard | last post by:
I was wondering if there are any open source compilers out there to translate the C++/Java/C#-like object model to C. Thanks, David
2
by: ds | last post by:
Hi all, what I try to do is the following: template<class Tpclass themap { public: typedef int Tp::*ptr; static std::map<std::string, Tp::ptrsmap;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.