typedef struct question

Started by
7 comments, last by LessBread 18 years, 1 month ago
What exactly is a struct and what does typedef do? I've seen it several times but I don't really know what exactly it does. Is it similar to classes?
Advertisement
A struct groups different variables together. A typedef defines a data type.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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.
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.
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.
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*
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++.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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 0x100typedef 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.
More fucking hair splitting... [flaming]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement