Originally Posted by
laserlight
More accurately, user is the name of an object of the type struct _user, an object that you defined even as you declared struct _user.
Right (I sometimes get mixed up with a "type declaration" versus "object 'of type' definition" with C structs). C style structs are a pretty confusing thing for someone just beginning I think. Especially with all the typedefs that most examples use ( and C++ handles it like that anyways which would increase the confusion ).
I think the confusion comes from the fact that the typedef version is usually indented like a regular struct declaration:
Code:
typedef struct Data
{
int a;
} Name;
// Makes more sense if you look at it like this:
typedef struct Data{ int a; } Name ;
In truth it might make more sense to uncouple the typedef from the declaration:
Code:
// Declaration of a user defined type called data:
struct Data
{
int a;
};
// a typedef which says the typename 'Name' is equivalent to declaration of object of type 'struct Data':
typedef struct Data Name;
For the OP, I updated the last example I posted to show how the typedef version can be used the same as the longer version:
Code:
#include <stdio.h>
#include <stdlib.h>
// Declaration of type
struct _data
{
int a;
} /* anything put here is a definition of an object of type _data, and is assignable to */;
// DATA becomes a more convenient typename
typedef struct _data DATA;
int getData( struct _data dat );
int main()
{
/* The next two lines are equivalent, but the typedef allows us to
declare 'dat' using the alias, which makes it seem more like a built in type. */
DATA dat;
struct _data dat2;
dat.a = 10;
dat2.a = 20;
printf("%d\n", getData( dat ));
printf("%d\n", getData( dat2 ));
return 0;
}
int getData( struct _data dat )
{
return dat.a;
}