Archived

This topic is now archived and is closed to further replies.

Typedef?

This topic is 6305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I was looking through the tutorials section, and came accross the following code:
  typedef struct MAPDATA_type { int xMax, yMax; // map size in tiles int xCamera, yCamera; // camera location in pixels int xMaxCamera, yMaxCamera; // max camera coordinates int nNPCCount; // number of NPCs int nScriptCount; // number of linked scripts LPNPC lpnpc; // linked NPCs LPSCRIPT lpscr; // linked scripts } MAPDATA, FAR* LPMAPDATA; 
huh? i dont get it. Did i mention my c/c++ book really sucks? It doesnt cover anything like this, and i''d kind-of like to know what it is, does, is used for, can be used for, etc... please...

Share on other sites
typedef defines a synonym for another type. It doesn''t create a new type, just an alias to an existing one. Like:

typedef unsigned long ulong;ulong ul; // Equivalent to "unsigned long ul;"

When you see a "typedef struct ...." you are defining a structure, and declaring aliases for it in the same block of code. Like:

typedef struct my_struct_tag{ int i; float f; char c;} my_struct, *pmy_struct;my_struct s; //declares a variable of type my_struct_tagmy_pstruct ps; //declares a pointer to a variable of type my_struct_tag

Share on other sites
i''m confused. slightly less than before though.

so if i declared a variable:

my_struct s;

what would ''s'' be? an integer? a float? a char? an array? all three (int float and char) combined into one??? in other words, whats the purpose of declaring variables inside it? and, assuming i had one, what would i do with the variables declared inside?

i''m downloading a c++ tutorial now, so maybe that will clear some stuff up... i sure wish my book included this stuff ...

heres a possible stupid question that may be answered in the tutorial i''m downloading: Whats a structure???

Share on other sites
Wow, your book must REALLY suck.

quote:
Original post by Tsu
what would 's' be?

It would be a structure of type my_struct (or, technically, my_struct_tag, but let's not confuse the issue).

quote:
Original post by Tsu
all three (int float and char) combined into one??? in other words, whats the purpose of declaring variables inside it? and, assuming i had one, what would i do with the variables declared inside?
...
Whats a structure???

These are all related, so... you basically defined a structure with "all three combined into one". It's a convenient way to create a complex data type, containing a number of variables that are usually related.

This is probably a little more complicated than what can be explained in a post, but if you look at the example you posted, you have a structure holding information about a map. If you look at the fields within the structure (nNPCCount, etc.), notice that they all are things you might want to know about a map. Since all these things are specific to ONE particular map, it's convenient to group them all together, rather than have a bunch of independent variables. Why is this convenient? Because you can declare an instance of MAPDATA (ie. a variable of type MAPDATA) like this:

MAPDATA map;

and then use map to access the fields within it, using the . (dot) operator; eg.

map.nNPCCount = 100;

if (map.nScriptCount)
...

etc.

In C++, structs become even more powerful, but again, I don't want to confuse the point.

Down with Tiberia!

Share on other sites
AHH, I thought your question was exclusively about typedef. =)

Anyway, a structure is just what it sounds like, it is a programming construct that is made up of other subcomponents. When you start talking about structures, you''re ''beginning'' to enter the realm of object oriented design. A few simple examples are:

struct CAR{int nMaxSpeed;int nFuelCapacity;char szName[32];};struct PERSON{char szName[64];int nAtheleticAbility;int nIQ;int nYearlySalary;int nAge;}

Each of the components IN a structure define that structure. Structures may contain properties only. Classes have the ability to also ''contain'' functions. Get comfortable with structures first, then take a little look at classes if you get curious, but it''s important to understand structures first.
};

Share on other sites
quote:
Original post by JonStelly

Structures may contain properties only. Classes have the ability to also ''contain'' functions.

Actually, that''s not true. In C++, structs can be used exactly like classes. The only difference is that the members of a struct are public by default, while the members of a class are private.

Share on other sites
Shhh, you''re confusing my point. =p

Share on other sites
yes, my book does really suck ... kind-of. it does explain most other things in detail (4 chapters on classes), but i think it was made to be at a ''beginners'' level (although it doesnt say it), which is probably why i bought it (okay, my parents bought it, but i picked it out ) in the first place. That and it was within my price range at the time. I guess you get what you pay for then.
i''m getting off topic...

quote:
Original post by JonStelly
AHH, I thought your question was exclusively about typedef. =)

Sorry to mislead anyone, but i didnt / don''t know the difference. (yet!)

quote:
Original post by JonStelly
Get comfortable with structures first, then take a little look at classes if you get curious, but it''s important to understand structures first.
When you start talking about structures, you''re ''beginning'' to enter the realm of object oriented design.

My book goes straight from strings and string related functions to classes. Its a bit confusing (to say the least).yes, i do plan on buying a better book(s) (as soon as i have some money )

BACK TO STRUCTS
So its kind-of like a database, (using my knowledge of MS Access as example here) where you have your table (MAPDATA), and the fields within the table(int xMax, etc)? but your only dealing with one record at a time (the var you assign to be a MAPDATA). or am i completely lost?

And i''m still a bit sketchy about typedefs... they just make an ''alias'' to the struct (using the first example)? is this almost the same as:
  #DEFINE BOB 42;...rectfill (buffer,BOB,...);

where wherever it finds ''BOB'', it replaces it with ''42''?

i guess what i should be asking is, what is meant by aliases?

if i know what your talking about, this will make sense:
  typedef struct my_struct_thing{ int x; int y; int q; char k[100]; //just for fun...}...my_struct_thing bob;...bob.x = 64;bob.y = 9;printf ("Bob has this much x: %d ", bob.x);

OUTPUT: Bob has this much x: 64
Correct?
if yes: YAY! i learned something!

i noticed that you (JonStelly) put a ''n'' in front of all your vars in the CAR and PERSON example (int nIQ. is that just for your own reference, or does that tell it its a number or something?

and finally, whats ''..} my_struct, *pmy_struct;..''? this is a slightly less important question, as i''m learning structs now, but i''m still curious...

Share on other sites
Ah, lots of questions...

First, to recomend a book that I really like / liked. It''s a great beginner / intermediate book called "Object Oriented Programming in C++" by The Waite Group.

Struct like a DB Table - Pretty Much... If thinking of them that way helps you, it''s close enough to the truth for now. Anything to help you visualize what you''re learning is a good thing IMHO.

typedef - The typedef code you''ve been looking at defines a structure and also creates the aliases. Let me simplify the example a bit by changing to a built-in type:

  typedef unsigned long u_long;u_long ulA;unsigned long ulB;

All the typedef does is give you another way to refer to the type. You could have done the same thing using:

  #define u_long (unsigned long)

but #define is EVIL, and with typedef, the compiler can perform some basic checks to make sure what you''re doing makes sense.

Variable Names - I use a modified version of "hungarian notation". It''s a system of naming your variables so that you can tell not only what they are used for, but also what type they are just by the name. Some examples:

char - cVariable //character
char[] - szVariable //string
char * - lpszVariable //pointer to a string
int - nVariable //integer
int * - pnVariable //pointer to an integer
float - fVariable //floating point integer
float * - pfVariable //pointer to a floating point integer
SYSTEMTIME * - pstCurrent //pointer to a SYSTEMTIME structure

and there are many more. I got used to it while I was learning how to program, and a lot of the microsoft samples etc... use it, which is my primary field, so I''ve just adopted my own style.

Share on other sites
thanks for the book recommend, i''m shopping for it right now in fact...

*Mental note: #DEFINE is evil... gotcha ...

ok, i think i get it now... thanks alots...

so then would this work?:
  typedef char[256] string;string my_string;

• 19
• 10
• 19
• 14
• 19