LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-11-2004, 06:19 AM   #1
brownstone
LQ Newbie
 
Registered: Jun 2004
Posts: 3

Rep: Reputation: 0
Can you help me with typedef?


hi

You can use typedef to create new data type names.But I'm wondering why we need typedef if you can use #define instead?Or is there something #define can't do but typedef can?

thank you
 
Old 06-11-2004, 07:25 AM   #2
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Rep: Reputation: 15
I might missed something, but I think you can't create data structures with define. The define just make a substitution.

As an example:

typedef struct {
double x;
double y;
} myType;


myType a,b;

Two instances are created (also memory is allocated therefore). I think doing something like this is not possible with define.
 
Old 06-11-2004, 07:43 AM   #3
crazyduck
LQ Newbie
 
Registered: Jun 2004
Location: poland
Distribution: slack
Posts: 5

Rep: Reputation: 0
Code:
#define smyType struct myType
 struct myType {
        double x;
        double y;
} ;

int main(void)
{
 smyType a,b;
 printf("it works :D \n");
}
~                                                                               
~
it works too, and it not require typedef

...but if the question is not "why we need typedef if you can use #define instead", but "why we need #define to define new types if you can use typedef instead?"

an the answer for YOUR question is:
* cos it's more easy to understand code wuthout too many #define's
* cos if you will migrate to c++ later your project can support namespaces with typedef (#define not)
* with #define it's quite easy to make mistake praticly not to solve by debug...
* and so on...

and now what about my question("why we need #define...")?

Last edited by crazyduck; 06-11-2004 at 07:48 AM.
 
Old 06-11-2004, 10:09 AM   #4
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
I actually thought the compiler could check typedefs, but not defines (due to that its a preprocessor macro). however I tested this with GCC by defining and typedefing two types (original int) and then I sent them as arguments to a function taking int. And GCC did not complain about either! ..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a question regarding typedef erikash Programming 1 11-10-2005 04:37 AM
typedef defines which type? nocturna_gr Programming 1 11-10-2005 02:46 AM
Typedef Problem in GCC in Linux ashwinipahuja Programming 4 04-26-2004 03:14 AM
explanation on typedef naren Programming 5 03-09-2004 10:48 PM
sizeof my typedef... jhorvath Programming 7 10-11-2002 05:41 PM


All times are GMT -5. The time now is 03:16 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration