Jo Loss Maths A Harry Carpenter English A Billy King Maths C |
The records above could be described in a struct as follows:
struct { char cname[8]; char sname[16]; char exam[16]; char grade; } record; |
The statement above declares a variable called record with 4 members called cname, sname, exam, grade. The structure as a whole can be referred to as record and a member can be referenced as record.exam
Structures can be declared in various forms...
struct x {int a; int b; int c;}; /* declaration */ struct {int a; int b; int c;} z; struct x z;All the examples above are structure declarations,
For example to assign a value, we can enter:
And to retrieve a value from a structure member:
All that we have discussed so far has been OK but runs into problems
when structures have to be moved between functions for the following
reasons.
So how does it all work? Here is an example. (make your browser W-I-D-E so
you can see the two examples).
Here is the annotation.
The :1 tells the compiler that only 1 byte is
required for Online and Mounted. There are a few points to
note about this though.
This is the most basic struct example I could think of.
Structure membership
We can access individual members of a structure with the . operator.
struct x {int a; int b; int c;};
main()
{
struct x z;
z.a = 10;
z.b = 20;
z.c = 30;
}
struct x
{
int a;
int b;
int c;
};
main()
{
struct x z;
z.a = 10;
z.a++;
printf(" first member is %d \n", z.a);
}
Pointers to structures
Fast path to an explanation of the -> operator.
|
|
struct x {int a; int b; int c;} ; | struct x {int a; int b; int c;} ;
|
void function(struct x); | void function(struct x *);
|
main() | main()
{ | {
struct x z; | struct x z, *pz; /* 3 */
| pz = &z; /* 4 */
z.a = 10; /* 1 */ | z.a = 10;
z.a++; | z.a++;
|
function(z); /* 2 */ | function(pz); /* 5 */
} | }
|
void function( struct x z) | void function(struct x * pz)
{ | { /* 6 */
printf(" first member %d \n", z.a);| printf(" first member %d \n", (*pz).a);
} | }
|
The (*pz).a syntax is used a great deal in C and it was decided to create
a short hand for it. So:
(*pz).a == pz->a
Here is the final picture.
/*************************************************************************/
struct x {int a; int b; int c;} ; /* Declare the structure. */
void function(struct x * ); /* Declare the function. */
/*************************************************************************/
main()
{
/* Declare two variables.
* z == type struct x
* pz == a pointer to type struct x
*/
struct x z, *pz;
pz = &z; /* put the address of 'z' into 'pz' */
z.a = 10; /* initialize z.a */
z.a++; /* Increment z.a */
/* print the contents of 'z.a'
* using the pointer 'pz' */
printf(" first member before the function call %d \n", pz->a);
/* Call 'function' passing the
* pointer 'pz' */
function(pz);
/* Print the NEW value of 'z.a'
* using three different notations */
printf(" first member after the function call %d \n", pz->a);
printf(" first member after the function call %d \n", (*pz).a);
printf(" first member after the function call %d \n", z.a);
}
/*************************************************************************/
void function(struct x * pz)
{
/* Print the value of 'z.a' by
* referencing the pointer 'pz'
* which holds the address of 'z' */
printf(" first member inside the function %d \n", pz->a);
/* Increment the value of 'z.a'
* this is the source location
* in memory. */
pz->a++;
}
/*************************************************************************/
The Bottom Draw
Finally, here is a little feature that allows you to save a little
space.
main()
{
struct Flags
{
unsigned int Online :1;
unsigned int Mounted :1;
}
struct Flags TapeInfo;
TapeInfo.Online = 1;
TapeInfo.Mounted = 0;
}
Examples
Using structure elements, and passing them into a function.
Passing a whole structure to a function. This performs a copy of the
structure so the same rules apply as for int etc.
Pointers to structures can be passed but I have not got to them yet....
Define and use an array of structures.
Here is a struct problem for you.
See Also:
typedef keyword.
Linked lists.
Top | Master Index | Keywords | Functions |