Results 1 to 4 of 4
i've the following structure definitions:
typedef struct
{
char infoType;
char infoLen;
char data[128];
}InfoType1;
struct InfoType2
{
char infoType;
char infoLen;
char data[128];
};
As far as I know, ...
- 09-25-2007 #1Just Joined!
- Join Date
- Sep 2007
- Posts
- 11
difference between "typedef struct" and "struct"
i've the following structure definitions:
typedef struct
{
char infoType;
char infoLen;
char data[128];
}InfoType1;
struct InfoType2
{
char infoType;
char infoLen;
char data[128];
};
As far as I know, differences between these two are:
--- when declaring of those types, we use
InfoType1 i1;
InfoType2 i2;
--- we can specify variables of type InfoType2 at the definition of struct itself like:
struct InfoType2
{
char infoType;
char infoLen;
char data[128];
}t1,t2;
But we can't specify variables of type InfoType1 at the definition of struct itself, a separate declaration is needed, like:
InfoType1 i1;
Are there any other differences between these two?
y do we've "typedef struct" when there is structure facility for a collection of variables of different data types?
- 09-28-2007 #2
I'm not sure what that final line in your post means.
But you have a misconception. The following will not compile:
Here's the error:Code:int main(void) { typedef struct { char infoType; char infoLen; char data[128]; } InfoType1; struct InfoType2 { char infoType; char infoLen; char data[128]; }; InfoType1 i1; InfoType2 i2; return 0; } /* main() */
InfoType2 is not a data type; it's something I would call a "struct type". The following code will compile without error:Code:f.c: In function `main': f.c:18: `InfoType2' undeclared (first use in this function) f.c:18: (Each undeclared identifier is reported only once f.c:18: for each function it appears in.) f.c:18: syntax error before "i2"
vijiambav, you want more information than can be easily presented in the context of a forum like this one. Please consider googling this:Code:int main(void) { typedef struct { char infoType; char infoLen; char data[128]; } InfoType1; struct InfoType2 { char infoType; char infoLen; char data[128]; }; InfoType1 i1; struct InfoType2 i2; return 0; } /* main() */
Hope this helps.Code:C tutorial
- 09-28-2007 #3
typedef isn't just for structs; I often find myself doing a
So I can declare stuff of type String. typedef effectively creates a new data type.Code:typedef char *String
- 09-28-2007 #4
Okay. The reason for this regards the way structs are handled.
For some reason, when you declare the following:
You must do this to recognize the type:Code:struct s1 { ... };
And so on. Note that you need to specify "struct s1". What we want to do is just say "s1". What typedef does is create a new data type that is exactly the same as another data type. For instance:Code:struct s1 myStruct; void myFunc(struct s1 variable) { ... }
We can now use "number" instead of "int" anywhere in the program. Similarly, we might do:Code:typedef int number
Now "s1" is equivalent to "struct s1" and can be used anywhere.Code:typedef struct s1 s1
But that's not all! Because we can declare anonymous structs, we can do the above in a single line:
This parses as follows:Code:typedef struct s1 { ... } s1
Note how "struct s1 { ... }" is considered the type that we are aliasing to, and s1 is the new alias.Code:[ typedef [ struct s1 { ... } ] s1 ]
Does this make sense?DISTRO=Arch
Registered Linux User #388732


