Structures: by Convention, The Name of The Structure Always Starts With A Big Letter
Structures: by Convention, The Name of The Structure Always Starts With A Big Letter
A structure is a collection of related variables under one name. It may contain variables of many different
data types.
struct structure_tag {
data_type var_name;
Data members
…
};
Where:
Structure tag – names the structured definition and is used with the keyword struct to declare
variables of the structure type.
Structure Members – the variables declared within braces of the structure definition. These
members are the attributes of the structure.
For example,
By convention, the name of the structure always starts with a big letter.
Note: Members of the same structures must have unique names, but two different structures may contain
members of the same name without conflict.
For example,
Based on the comparison, both objects have the same attributes (i.e. id number, age and name) and only
differ on the fourth attribute that is tuition fee for student and salary for employee.
struct Employee{
int idnumber;
char name[30];
int age;
float salary;
};
These two structures will work without conflict since to refer to a structure member, you need to refer
first to the structure where it belongs to.
For example,
struct Student s;
Variable s is the structure variable. To declare, always specify the name of the structure with the struct
keyword. With the above example, s is allocated memory with the size of struct Student. Below is an
illustration of s If using the previous definition of struct Student.
s
idnumber name age tuitionfee
The Use of typedef
Since to declare a structure variable you always need to have the struct keyword and the structure tag,
there would always be two words before the variable. So to simplify, we can use typedef.
The keyword typedef provides a mechanism for creating synonyms (or aliases) for previously defined data
types. There are three ways to use typedef for structures. Here are the examples:
1. typedef struct Student Stude;
2. typedef struct Student{
int idnumber;
char name[30];
int age;
float tuitionfee;
}Stude;
3. typedef struct {
int idnumber;
char name[30];
int age;
float tuitionfee;
}Stude;
When using the third example, the structure tag becomes optional. In this way, there is only one name to
use in declaring a structure variable, that is Stude.
From the above examples, Stude is not a variable name but an alias for struct Student. So instead of
declaring,
struct Student s;
We can use,
Stude s;
Note: Creating a new name with typedef does not create a new data type; typedef simply creates a new
type name, which may be used as an alias for an existing type names.
Initializing Structures
Structures can be initialized using initializer lists as with arrays. To initialize a structure, follow the variable
name in the structure declaration with an equal sign and a brace-enclosed, comma-separated list of
initializers.
For example,
Stude s = {101, “Jose Rizal”, 18, 24500.25};
To illustrate:
However, remember to initialize with values that correctly match the data type. Otherwise, an error may
occur.
structureVariable.memberName
For example,
s.age = 20;
strcpy(s.name,“Marlowe”);
printf(“%d”,s.age);
printf(“%s”,s.name);
The arrow operator is used when using a pointer to indirectly access the structure members.
structurePointer->memberName
For example:
struct Student s, *sptr;
sptr=&s;
sptr->age=20;
strcpy(sptr->name,”Marlowe”);
Nested Structures
For example,
struct Birth{
int month, day, year;
};
struct Stud{
char name[20];
int age;
float grade;
struct Birth bday;
};
struct Stud s;
Structure variable bday is of type struct Birth. How can we access an inner structure variable?
1. x.bio.idno=1012; //invalid
4. x.Bd.month=6; //valid
5. bio.birth.year=1975; //invalid
When structures or individual structure members are passed to functions, they are passed by value.
Here is an example:
To pass a structure call by reference, pass the address of the structure variable.
Here is an example:
From the above example, since input() has to accept new values for s then it needs to pass s by reference.
Thus, the function uses a pointer as its parameter and the arrow operator to access the elements.