D
Darklight
What is written below i will use as a quick reference i just want to make
sure i have got it right
/* STRUCTURES */
TO DEFINE A STRUCTURE
way 1. To declare a structure and instance/s together.
struct coord{ /* struct keyword is used to declare a stucture, coord
is the structure name or tag */
int x;
int y; /* x & y are the names of the member variables of the
structure */
} first, second; /* first & second are the instances of the structure */
/* each instance contains two integers */
WAY 2. TO DECLARE INSTANCE IN A DIFFERENT LOCATION
struct coord{
int x;
int y;
};
/* additional code goes here */
struct coord first, second;
ACCESSING STRUCTURE MEMBERS.
first.x = 50;
first.y = 100; /* to access a structure member you have to use the
dot (.) operator between */
/* the structure name and the member name */
WAY 3. TO DEDCLARE AND INITIALIZE A STRUCTURE.
struct time{
int hours;
int minutes;
int seconds;
}time_of_birth = {8,45,0};
TO COPY INFORMATION BETWEEN STRUCTURES.
first = second;
first = second;
the above is equivalent to
first.x = second.x;
first.y = second.y;
STRUCTURES THAT CONTAIN ARRAYS
struct data{
int x[4];
char y[10]
}record;
record.y /* IS A POINTER TO THE FIRST ELEMENT OF THE ARRAY */
ARRAYS OF STRUCTURES
struct entry{
char fname[10];
char lname[12];
char phone[8];
};
struct entry list[100]; /* declaring an array of structures of type entry */
/* each element is a structure of type entry */
/* each element of list array has three elements */
/* IE: first element list[0] = list[0].fname,
list[0].lname, list[0].phone */
/* last element list[999] = list[999].fname,
list[999].lname, list[999].phoone */
INITIALIZING STRUCTURES
struct customer{
char firm[20];
char contact[25];
};
struct sale{
struct customer buyer; /* structure to a structure */
char item[20];
float amount;
};
struct sale y1990[100] = { /* initializing the structures */
{{ "acme industries", "George Adams"},
"left-hand widget",
1000.00
},
{{ "Wilson & co.","Ed Wilson"},
"Type 12 gizmo",
200.00
}
};
TO DISPLAY THE ABOVE STRUCTURES
for(i = 0; i < 2; i++)
{
printf("%s %s\n %s £%.2f\n\n", y1990.buyer.firm,
y1990.buyer.contact, y1990.item, y1990.amount);
}
POINTERS AS STRUCTURE MEMBERS
struct data{
int *value;
int *rate
}first; /* You must initialize pointers to point at some
thing */
/* to do this you do as below */
first.value = &cost;
first.rate = &interest; /* cost and interest must have been declared as int variables */
/* When using char variable do not use the &
operator */
POINTERS TO STRUCTURES
struct part{ /* define a structure */
int number;
char name[10];
};
struct part *p_part; /* declare a pointer to type part;
struct part gizmo; /* declare an instance to type part */
p_part = &gizmo; /* perform pointer initialization */
(*p_part).number = 100; /* this assigns the value 100 to
gizmo.number*/
/* this uses the indirection operator (*)*/
p_part->number /* this is used to access structure members
using a pointer */
/* this uses the indirect membership operator
(->) */
THREE WAYS TO ACCESS A STUCTURE MEMBER
str.memb;
(*p_str).memb;
p_str->memb;
POINTERS AND ARRAYS OF STRUCTURES
struct part{ /* define struture */
int number;
char name[10];
};
struct part data[100]; /* declare an array of type part */
struct part *p_part; /* declare a pointer to type part */
p_part = &data[0]; /* initialize pointer to point to first element in array */
p_part = data; /* second way to do above */
printf("%s %s", p_part->number, p_part->name);
/* to print contents of first element */
PASSING STRUCTURES AS ARGUMENTS TO FUNCTIONS
struct data{ /* define and initialize a structure */
float amount;
char fname[30];
char lname[30];
}rec;
void print_rec(struct data x); /* declare function prototype*/
/* addditional statements go here */
print_rec(rec); /* call function */
void print_rec(struct data x) /* declare or define function definition */
{
printf("\nDoners name %s %s gave £%.2f\n",x.fname,
x.lname,x.amount);
}
sure i have got it right
/* STRUCTURES */
TO DEFINE A STRUCTURE
way 1. To declare a structure and instance/s together.
struct coord{ /* struct keyword is used to declare a stucture, coord
is the structure name or tag */
int x;
int y; /* x & y are the names of the member variables of the
structure */
} first, second; /* first & second are the instances of the structure */
/* each instance contains two integers */
WAY 2. TO DECLARE INSTANCE IN A DIFFERENT LOCATION
struct coord{
int x;
int y;
};
/* additional code goes here */
struct coord first, second;
ACCESSING STRUCTURE MEMBERS.
first.x = 50;
first.y = 100; /* to access a structure member you have to use the
dot (.) operator between */
/* the structure name and the member name */
WAY 3. TO DEDCLARE AND INITIALIZE A STRUCTURE.
struct time{
int hours;
int minutes;
int seconds;
}time_of_birth = {8,45,0};
TO COPY INFORMATION BETWEEN STRUCTURES.
first = second;
first = second;
the above is equivalent to
first.x = second.x;
first.y = second.y;
STRUCTURES THAT CONTAIN ARRAYS
struct data{
int x[4];
char y[10]
}record;
record.y /* IS A POINTER TO THE FIRST ELEMENT OF THE ARRAY */
ARRAYS OF STRUCTURES
struct entry{
char fname[10];
char lname[12];
char phone[8];
};
struct entry list[100]; /* declaring an array of structures of type entry */
/* each element is a structure of type entry */
/* each element of list array has three elements */
/* IE: first element list[0] = list[0].fname,
list[0].lname, list[0].phone */
/* last element list[999] = list[999].fname,
list[999].lname, list[999].phoone */
INITIALIZING STRUCTURES
struct customer{
char firm[20];
char contact[25];
};
struct sale{
struct customer buyer; /* structure to a structure */
char item[20];
float amount;
};
struct sale y1990[100] = { /* initializing the structures */
{{ "acme industries", "George Adams"},
"left-hand widget",
1000.00
},
{{ "Wilson & co.","Ed Wilson"},
"Type 12 gizmo",
200.00
}
};
TO DISPLAY THE ABOVE STRUCTURES
for(i = 0; i < 2; i++)
{
printf("%s %s\n %s £%.2f\n\n", y1990.buyer.firm,
y1990.buyer.contact, y1990.item, y1990.amount);
}
POINTERS AS STRUCTURE MEMBERS
struct data{
int *value;
int *rate
}first; /* You must initialize pointers to point at some
thing */
/* to do this you do as below */
first.value = &cost;
first.rate = &interest; /* cost and interest must have been declared as int variables */
/* When using char variable do not use the &
operator */
POINTERS TO STRUCTURES
struct part{ /* define a structure */
int number;
char name[10];
};
struct part *p_part; /* declare a pointer to type part;
struct part gizmo; /* declare an instance to type part */
p_part = &gizmo; /* perform pointer initialization */
(*p_part).number = 100; /* this assigns the value 100 to
gizmo.number*/
/* this uses the indirection operator (*)*/
p_part->number /* this is used to access structure members
using a pointer */
/* this uses the indirect membership operator
(->) */
THREE WAYS TO ACCESS A STUCTURE MEMBER
str.memb;
(*p_str).memb;
p_str->memb;
POINTERS AND ARRAYS OF STRUCTURES
struct part{ /* define struture */
int number;
char name[10];
};
struct part data[100]; /* declare an array of type part */
struct part *p_part; /* declare a pointer to type part */
p_part = &data[0]; /* initialize pointer to point to first element in array */
p_part = data; /* second way to do above */
printf("%s %s", p_part->number, p_part->name);
/* to print contents of first element */
PASSING STRUCTURES AS ARGUMENTS TO FUNCTIONS
struct data{ /* define and initialize a structure */
float amount;
char fname[30];
char lname[30];
}rec;
void print_rec(struct data x); /* declare function prototype*/
/* addditional statements go here */
print_rec(rec); /* call function */
void print_rec(struct data x) /* declare or define function definition */
{
printf("\nDoners name %s %s gave £%.2f\n",x.fname,
x.lname,x.amount);
}