Sign in to follow this  

typedef struct question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

In general, typdefing a struct is legacy C style code to avoid having to say


struct SomeStruct{/* ... */};
struct SomeStruct variable;




You have to explicitly tell a c compiler you are using structs. In c++ you don't.

so they typedef it to something like this:


struct SomeStruct_TAG{/* ... */};
typedef SomeStruct_TAG SomeStruct;
SomeStruct variable;




then they stick it in one line because "why make a program readable?".( yeah, I don't know why they do it )


typedef struct SomeStruct_TAG{/* ... */}SomeStruct;
SomeStruct variable;




And the above probaly isn't right, I never use this syntax.

Share this post


Link to post
Share on other sites
I assume C++?

A struct is just a class, except the default access level is public.

A typedef is a way to rename a type name. For instance:
typedef std::basic_string< TCHAR > tstring;


jfl.

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
A struct groups different variables together. A typedef defines a data type.


Unless he's talking about C++, then structs are just classes with public access as the default, instead of private.

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy
Quote:
Original post by LessBread
A struct groups different variables together. A typedef defines a data type.


Unless he's talking about C++, then structs are just classes with public access as the default, instead of private.

I find 'group of variables' to be more explaining than 'classes with public access', from a noob point of view.
Well. *shrugs*

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy
Quote:
Original post by LessBread
A struct groups different variables together. A typedef defines a data type.


Unless he's talking about C++, then structs are just classes with public access as the default, instead of private.


Since he asked about structs and typedefs, it seems more likely that he's asking about straight C - even if he asked if they were similar to classes. Tell me though, does a struct group different variables together under C++ or does it do something different? To be more straight forward, my answer may have been simplistic, but it wasn't wrong. A struct groups different variables together in C and in C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
A typedef defines a data type.


I wish it did. But unfortunately a typedef only defines a new name for an existing data type. For example:

/* in my headers */
#define SOME_FLAG_SETTING 0x100
typedef unsigned int flag_t;

/* somewhere in my code */
unsigned int flag = SOME_FLAG_SETTING;
do_something_with_flags(flag, 200);

/* somewhere else */
void do_something_with_flags(unsigned int count, flag_t flag)
{
/* ... */
}

In this example, the compiler will let me pass an unsigned int to do_something_with_flags, despite the fact that I've declared do_something_with_flags to accept an argument of type flag_t. This behaviour is, unfortunately, perfectly valid, because flag_t is really only a new name for an unsigned int.

If you do want typesafety in this sort of case, you can have it, but you need to do a bit more work:

/* in my headers */
#define SOME_FLAG_SETTING {0x100}
typedef struct { unsigned int flag; } flag_t;

/* somewhere in my code */
unsigned int flag = SOME_FLAG_SETTING;
do_something_with_flags(flag, 200);

/* somewhere else */
void do_something_with_flags(unsigned int count, flag_t flag)
{
/* ... */
}

In the above code, the compiler will raise an error indicating that we're attempting to pass an incompatible type as the second argument of do_something_with_flags.

You can also achieve something similar using enums in C, and C++ presents many more opportunities for writing typesafe code.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this