Typedef?

Started by
16 comments, last by Tsu 23 years, 1 month ago
go here and read it:
http://www.edm2.com/0408/introc1.html

a very good intro to C, it is only 30 pages but it is about equivilent to a semester
Advertisement
Tsu, #define is NOT evil. There are many legitimate reasons for using #define and other preprocessor directives.

For example:

1. Stopping the redefinitions when including the same header file in two different files
2. Creating code that does extra error checking only while in debug mode (ie, ASSERTS)
3. Using platform specific code in a multi-platform application.
4. Getting file and line numbers on errors for debugging purposes.
5. Overriding compiler specific byte alignment so your code works on all compilers.

The list goes on and on. However, you SHOULDN''T use #defines when you can do the same thing using normal C/C++ code, as in JonStelly''s example.


- Houdini
- Houdini
Tsu,

I think a lot of your confusion arises from the fact that typedef and struct are used together in the example, and because of differences between C and C++.

In C you had to use the struct keyword when defining structs and declaring variables based on those structs. In C++ there is no need to include the struct keyword when declaring structure type variables. For example:

struct PERSON_TYPE{  int Age;  int Height;  char Name[64];};  struct PERSON_TYPE Player1; // This is ok in C and C++PERSON_TYPE Player2; // Illegal in C, but ok in C++  


Typedef lets you assign other names to types. To save having to include the struct keyword before every declaration of a PERSON_TYPE variable you could typedef it like this:

struct PERSON_TYPE{  int Age;  int Height;  char Name[64];};  typedef struct PERSON_TYPE PERSON;  PERSON Player1;PERSON Player2;   


It is possible to combine typedef and struct, defining your structure as you typedef it, which is what is happening in your MAPDATA example. My example could look like this:

typedef struct PERSON_TYPE{  int Age;  int Height;  char Name[64];}PERSON;    PERSON Player1;PERSON Player2;   


However, there is no need to do this if you're using C++. You can just do this:

struct PERSON_TYPE{  int Age;  int Height;  char Name[64];};    PERSON_TYPE Player1; // This only works in C++PERSON_TYPE Player2;   


Just to confuse matters, you can also include a list of variables immediately after a struct definition, like this:

struct PERSON_TYPE{  int Age;  int Height;  char Name[64];}Player1, Player2; // ok in C and C++  Player1.Age = 24;//.. etc...  


Note that this is very different from the typedef example. This defines Player1 and Player2 as variables of type PERSON_TYPE.

I know it's confusing but if you're using C++ I'd suggest forgetting about typedef, other than to use it for convenience/readability sake with things like "typedef unsigned long DWORD". There's no need to use it with structs.

Hope this makes sense.

Moot


Edited by - Moot on February 15, 2001 9:20:03 AM
Ok, if this is a repeat of anything already said, forgive me.

Alright, let''s say you have some kind of graphics function. Now, you want to pass it an x and y coordinate, to start drawing at. You also have a BUNCH of other similar functions. But, you don''t want to go to all the trouble of passing int x, int y, to every function. This is why we use structs. Probably the simplest struct you will ever see is a Point, or Coordinate struct. Typically programmers use Point. What it does is creates a new variable type called Point, that *contains* two variables called X, and Y. Check it out:

// Struct is the keyword, Point is the new variable-type-name.
struct Point
{
// X and Y are "sub-variables", usually called "members".
int X;
int Y;
};

Ok, now you have a Point type. So what do you do with it? You use it just like an int or char or any of the built-in types.

Point p;
Point q;
Point w;

But what does that get you? Well, so far, nothing. But, that''s because you don''t know how to access X and Y.

p.X = 4;
p.Y = 40;

q.X = 10;
q.Y = 11;

w.X = p.X + q.X;
w.Y = p.Y + q.Y;

So know, i think it should be clear what they do. That''s all for now.

farmersckn
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
The book Object Oriented Programming in C++ is a _very_ good book for learning about this sort of thing. My thanks to you, JonStelly, for suggesting it

Tsu - Thanks for posting a question I had never even thought about before, and I hope you''ve got it figured out... Good luck

Faradhi Sobriet-Treves
Press to test... *click* Release to detonate...
Press to test... *click* Release to detonate...
Moot: Wow, I knew both of those things about typedef and struct in C, but I never put two and two together, so I never understood why they used all the typedef-ing.

Tsu: I can''t offer anything that hasn''t been said, but good luck learning C++!
quote:Original Posting by Faradhi
Tsu - Thanks for posting a question I had never even thought about before, and I hope you''ve got it figured out... Good luck


No problem! i had no idea my confusion could actually help people ... thanks

Erase Mental Note: #DEFINE is evil
Replace Mental Note: #DEFINE is evil when used improperly

so, what good is define? Houdini, you mentioned:
"Stopping the redefinitions when including the same header file in two different files"... What do you mean by that? I can make a header file that declares a variable inside it, but then it says that the variable name is already declared. Is that what you mean?

So as i see them, structs are like databases (well, one record of them anyway, depends on how you use them). Somebody, i dont remember who and am too lazy to scroll down and find out, hinted that they can be used for more than that... i''m curious.

quote:
struct PERSON_TYPE Player1;

This seems like a wierd way of declaring a variable though... wouldnt that make a new struct instead of declaring a var as one though? it just seems like it would... i''ll just switch to ''gxx'' when i compile then (i currently use ''gcc'')... would there be any problems / major differences if i do that...

RE: THAT BOOK
Hurrah for books that arent really expesive!
the ones that i could''v chosen over the one i have now where about $90 - $150... i chose the $60 one, as i only had that much money...
The macros that you manipulate with #define, #undef, #ifdef, #else, etc. are preprocessor macros. They are not part of the language. They are extremely common, in fact so much so that you can count on them in any C/C++ compiler.

A source file is just a text file. Those preprocessor macros just tell the compiler to skip, replace, etc. certain portions of text (not code), so they can be used to do things that you can''t normally do in code, like generate incomplete blocks of code to be used as building blocks for complex functions.

After the preprocessor is done, the compiler looks at the modified text file.

Another use for the preprocessor is conditional compilation. If you have one part of an optimized function written in code that works well on Alpha processors, then you can direct the compiler to use that code when compiling your program for Alpha processors. And you could have it use another set of code for Intel processors.

You accomplish conditional compilation by first checking for some kind of condition (say the target platform, or the version of the compiler), and then defining a symbol. Code that should or shouldn''t be compiled then checks whether or not that symbol was defined:



// included somewhere like in the project''s main header
#ifdef _WINDOWS
#ifndef _DLL
#define WINDOWS_APPLICATION
#else
#define WINDOWS_DLL
#endif

// much later, in many many places
window::~window()
{
#ifdef WINDOWS_DLL
// Requires different cleanup for DLLs
UnregisterClass(...);
#endif
}



Tools are never evil; only misuse of tools is evil.

This topic is closed to new replies.

Advertisement