A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
A point is nothing but a pair of coordinates and a collection of points makes a rectangle.
struct
keyword is used.
struct point {
int x;
int y; //members
};
An optional name called a structure tag may follow the word struct
.
struct point {
int x;
int y;
} x , y, z;
struct
type variables.
struct point pt; //pt's type is struct point
//initialization can be done by
struct point pt = { 320, 200 };
structure-name . member
struct point pt;
int a = pt.x;
int b = pt.y;
struct rect {
struct point pt1;
struct point pt2;
};
//we can declare screen as
struct rect screen;
//then
screen.pt1.x;
The only legal operations on structures are -
Structure parameters are passed by value.
We can pass a pointer to a structure.
struct point *pp;
struct point origin;
*pp = &origin;
//to access elements
(*pp).x;
// instead of writing this
(*pp).x;
// we can access members like this
pp -> x;
->
operator is the Highest.
struct point {
int x;
char *str;
} *p;
++ p -> x; // this increments x, not p
* p -> str; // this accesses location popinted to by str
struct key {
int count;
char *word;
} keytab[NKEYS];
struct
.struct key *binsearch(int x, int y);
struct {
char c;
int i;
}; //its size can be 8, not 5
sizeof()
returns the exact value.
struct node {
int value;
struct node *left;
struct node *right; //referring to self
}
When a line like #define IN 1
is encountered, the name IN and the replacement text 1 are stored in a table. Later, when the name IN appears in a statement like state = IN;
it must be replaced by 1
.
There are two routines that manipulate the names and replacement texts. install(s,t)
records the name s
and the replacement text t
in a table; s
and t
are just character strings. lookup(s)
searches for s
in the table, and returns a pointer to the place where it was found, or NULL
if it wasn’t there.
The algorithm is a hash-search - the incoming name is converted into a small non-negative integer, which is then used to index into an array of pointers. An array element points to the beginning of a linked list of blocks describing names that have that hash value. It is NULL
if no names have hashed to that value.
struct nlist { /* table entry: */
struct nlist *next; /* next entry in chain */
char *name; /* defined name */
char *defn; /* replacement text */
};
typedef
.typedef int Length;
In the program that follows, we can use Length
in place of int
as a new type.
Length x, y;
typedef
in C basically works as an alias.
typedef
can be used to alias compound data types such as struct and union.typedef
can be used to alias both compound data types and pointer to these compound types.typedef
can be used to alias a function pointer.typedef
can be used to alias an array.union u_tag {
int ival;
float fval;
char *sval;
} u;
Accessing of members can be done as - union-name . member
or union-pointer -> member
.
The size of a union is big enough to hold the “widest” number.
Operations permitted are same as those of structures.
A union may only be initialized with a value of the type of its first member; thus union u
described above can only be initialized with an integer value.
struct {
unsigned int is_keyword : 1;
unsigned int is_extern : 1;
unsigned int is_static : 1;
} flags;
The number following the colon represents the field width in bits.
Individual fields are referenced in the same way as other structure members: flags.is_keyword
, flags.is_extern
, etc.
Almost everything about fields is implementation-dependent.