'struct'
Recommended Reading:
K&R section 6.1
Definition
A structure defines a new data type, made up of elements called 'members.'
Each member within a struct is identified with a unique name and may be
of any type.
struct structure_tag {
type1 name1;
type2 name2;
etc...
} optional_variable_list ;
example:
struct point {
float x;
float y;
};
struct point first; /*defines a struct point named first */
first.x = 2.3; /* use '.' to select a member of a struct */
first.y = 6.7; /* first is the point (2.3, 6.7) */
You can also define variables when you define the structure:
struct point {
float x;
float y;
} first, second;
same as...
struct point first;
struct point second;
other ways to assign values to struct variables:
struct point first = { 2.3, 6.7 };
struct point second = first;
NOTE: Structure members do not have to be all the same type.
Can you think of an application for which a struct would be appropriate?
'typedef'
Recommended Reading:
K&R section 6.7
Definition
Typedef defines a new data type by giving a new name to an already
defined type.
typedef old_name new_name;
example:
typedef int number; /*'number' is now a defined type */
number count; /* 'count' is a variable of type number (int) */
typedef often used in conjunction with struct:
typedef struct { float x; float y; } point;
point first, second; /* 2 variables of type point */
point triangle[3]; /* array of 3 points */
triangle[0].x = 0.0;
triangle[0].y = 0.0;
etc...
Question: can you explain the difference in meaning between:
q[0].a = 5;
and
q.a[0] = 5;
More specifically, what types are variables q and a?