Identifying the parts of this typedef struct in C?

Go To StackoverFlow.com

4

Please help me identify the parts of this typdef and what each part does and how it can be used:

typedef struct my_struct
{
        int a;
        int b;
        int c;
} struct_int, *p_s;

struct_int struct_array[5];

What I think they are, pleae correct if wrong:

  1. typedef struct is creating a new type.
  2. my_struct is the name of this type but not used in the rest of the code.
  3. struct_int is one instance of the type that we can use in the code.
  4. *p_s is the pointer specifically to the one instance we created.
  5. struct_array is an array of the instance we created. (This part confuses me since we already created an instance...)

Also, when creating the array of structs, why do we use struct_int instead of my_struct?

2012-04-05 23:24
by T.T.T.
We generally like helping people with homework; I'm afraid this is getting close to doing your homework. Can you fill in what your guesses are for the different portions - sarnold 2012-04-05 23:26
sure, please hold.. - T.T.T. 2012-04-05 23:27
@sarnold: nice resume........ - T.T.T. 2012-04-05 23:37
Much better, thanks! (And thanks! : - sarnold 2012-04-05 23:37


3

typedef struct is creating a new type.

The typedef is defining struct_int and p_s, which are synonymous to struct my_struct and struct my_struct* respectively. The idea is to save you from having to type things out the long way and to improve readability (which isn't always the case).

my_struct is the name of this type but not used in the rest of the code.

Because you would have to use my_struct as struct my_struct, but you've typedef'd struct_int to refer to that type and it's simpler.

struct_int is one instance of the type that we can use in the code.

No, read the above. If you removed the typedef, then you would be creating two variables struct_int and p_s.

*p_s is the pointer specifically to the one instance we created.

No, read the above.

struct_array is an array of the instance we created. (This part confuses me since we already created an instance...)

struct_array is an array of type struct_int, therefore it's an array of type struct my_struct. No instance is created in the definition of your struct.

Also, when creating the array of structs, why do we use struct_int instead of my_struct ?

Same as one of the questions above; you can use struct my_struct or struct_int, struct_int is shorter and it'd defeat the purpose of typedefing struct_int.

2012-04-05 23:40
by AusCBloke
nicely explained, thank you very much - T.T.T. 2012-04-05 23:46


4

struct my_struct is a name of a type, my_struct is a tag name and not a type.

typedef does not create types in C, but creates alias names for existing types.

2012-04-05 23:34
by ouah
creates alias is the part I was missing, thank - T.T.T. 2012-04-05 23:52


3

Not quite! Here's what different part mean:

struct my_struct: Here you are creating a new data type which you will refer to as struct my_struct whenever you want to create a new instance of the object. For example you should say: struct my_struct a; and not my_struct a;

typedef struct my_struct {} struct_int;: Here you merely create an alias for struct my_struct and name it struct_int. From now on, you can say struct_int a; instead of struct my_struct a;

typedef struct my_struct {} *p_s; Here, you create a new alias for a data type that points to struct my_struct. You can use it like this: p_s newStruct = malloc(sizeof(struct_int));

2012-04-05 23:38
by GradGuy
@sarnold Thank - GradGuy 2012-04-05 23:39


2

my_struct is the name of this type of structure. It can later be used to declare more structures of this type.

struct_int is the name of the alias created by typedef.

*p_s is a pointer to a structure of the my_struct (aka struct_int) type. It can point to a structure of this type.

struct_array is the name of an array of my_structs. However, since it is declared with the alias struct_int, we should refer to it later as one.

Using the alias is a personal preference, really one or the other of my_struct or struct_int could be removed.

2012-04-05 23:38
by Caleb


1

Your assumptions are partly correct. I'd like to go into 3. and 4.

If you removed the typedef and had this:

struct my_struct {
  // ...
} struct_int_v, *p_s_v;

Then you would have declared two variables struct_int_v and p_s_v with the types you expected (one being struct my_struct and the other struct my_struct*).

However, when introducing the stroage specifier typedef you are telling C not to create a variable but a type, while the declaration syntax is identical.

Thus, struct_int will be the type that the variable struct_int_v would have and p_s will be the type that the struct_int_v variable would have.

Note that storage specifiers like typedef (or static for that matter) apply to all variables (while other qualifiers like e.g. const or volatile apply to types in contrast) up to ;.

2012-04-05 23:44
by bitmask
"you are telling C not to create a variable but a type, while the declaration syntax is identical." - didn't know that, thank - T.T.T. 2012-04-05 23:48


1

You mostly answered your own question, so I'll address the two remaining points I see:

First, the name of the type that the struct definition creates is struct my_struct. You could declare variables like

struct my_struct a;
struct my_struct b;

but you don't want to have to write struct all the time so you create an alias using typedef. It might be a little clearer to define the structure and the aliases separately:

struct my_struct {
  int a, b, c;
};
typedef struct my_struct struct_int;
typedef struct my_struct *p_s;

This first defines a type called struct my_struct, then defines struct_int as an alias for the type struct my_struct, then defines p_s as an alias for the type struct my_struct *. The code you wrote has all three of these things combined into one, but it means the same thing.

Second, the type alias p_s has nothing to do with the array struct_array. The *p_s doesn't define an instance of the structure, because it's part of a typedef: it defines a type alias instead. But even if there were no typedef, and p_s were therefore an actual pointer variable, the array defined afterward is a separate and unrelated variable.

2012-04-05 23:44
by Wyzard
I like how you made it cleaner.. - T.T.T. 2012-04-05 23:51
When using typedef to create convenience aliases for structure types, it's common to omit the structure name entirely: typedef struct { /* ... */ } my_struct_type;. No sense having two different names for the same thing - Wyzard 2012-04-05 23:56
You can also directly create instances of anonymous structures: struct { int a, b; } my_two_ints;. Since there's no typedef, this defines an actual variable - Wyzard 2012-04-05 23:57
interesting, that makes sense - T.T.T. 2012-04-05 23:57
Ads