S.O.S. Mathematics CyberBoard

Your Resource for mathematics help on the web!
It is currently Fri, 24 May 2013 11:48:23 UTC

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Help with C please
PostPosted: Fri, 13 May 2005 15:28:53 UTC 
Offline
Member of the 'S.O.S. Math' Hall of Fame

Joined: Sun, 6 Jul 2003 07:53:15 UTC
Posts: 409
Location: Melbourne, Australia
I just have a quick question about C. I don't understand the primary purpose of 'typedef'. I have read my textbook but I still don't have a clear idea as to what typedef does. From what I gather(in my book typedef is introduced in the context of arrays), typedef defines a type and acts as a kind of definition shortcut in programs. That's all I (think)understand about it. Can someone please give me an explanation. I'm finding it to be quite difficult to understand and make use of - even after having read my textbook a few times.


Top
 Profile  
 
 Post subject:
PostPosted: Fri, 13 May 2005 17:58:09 UTC 
Offline
Member of the 'S.O.S. Math' Hall of Fame

Joined: Thu, 9 Oct 2003 08:09:39 UTC
Posts: 1631
If I recall correctly, typedef is basically a way for you to define something that is very long to type. That way once you defined it by typedefing it, you won't have to type that same long thing over and over again. It's used in creating structs, say if your struct is very huge (30+ members). Do you really want to type that all over your code?


Top
 Profile  
 
 Post subject:
PostPosted: Fri, 13 May 2005 18:48:56 UTC 
Put simply, typedef is just a way to create a new data type name. As Corneo said, this is useful with structs:

Code:
typedef struct { int age; char* name } person;


Remember by default structs have public access so:

Code:
person a;
a.age = 19;
a.name="Conrad";


would create me... and so on, you get it. :)

-- X Conrad X


Top
  
 
 Post subject:
PostPosted: Fri, 13 May 2005 18:52:14 UTC 
Offline
Member of the 'S.O.S. Math' Hall of Fame
User avatar

Joined: Wed, 1 Oct 2003 04:45:43 UTC
Posts: 9633
Corneo wrote:
If I recall correctly, typedef is basically a way for you to define something that is very long to type.

Another use for typedef is to define mnemonics for the standard data types. For example, you might do:

Code:
typedef unsigned int natural;

or

Code:
typedef double real;

Then in your code, you could write things like:

Code:
real x, y;
natural m, n;

which is easier to read, I suppose.


Top
 Profile  
 
 Post subject:
PostPosted: Sat, 14 May 2005 06:39:37 UTC 
Offline
Member of the 'S.O.S. Math' Hall of Fame

Joined: Sun, 6 Jul 2003 07:53:15 UTC
Posts: 409
Location: Melbourne, Australia
Thanks for the help. So for your example if say I wrote 'real g' then the compiler would take that 'g' as a variable of type double?


Top
 Profile  
 
 Post subject:
PostPosted: Sat, 14 May 2005 07:45:23 UTC 
yes.

-- X Conrad X


Top
  
 
 Post subject:
PostPosted: Sat, 14 May 2005 09:40:25 UTC 
Offline
Member of the 'S.O.S. Math' Hall of Fame

Joined: Sun, 6 Jul 2003 07:53:15 UTC
Posts: 409
Location: Melbourne, Australia
Thanks GOD.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Contact Us | S.O.S. Mathematics Homepage
Privacy Statement | Search the "old" CyberBoard

users online during the last hour
Powered by phpBB © 2001, 2005-2011 phpBB Group.
Copyright © 1999-2013 MathMedics, LLC. All rights reserved.
Math Medics, LLC. - P.O. Box 12395 - El Paso TX 79913 - USA