Professional Documents
Culture Documents
Lecturer (EE)
Institute of Space Technology, Islamabad
muhammad.asad@ist.edu.pk
sizeof() Operator
• Unary operator
• Compile time operator
• Determine Size in bytes of data types and arrays during program
compilation.
struct structName
{
dataType1 identifier1;
dataType2 identifier2;
.
.
.
dataTypeN identifierN;
};
Example
struct studentType
{
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
};
struct Variable Declaration;
• Int a;
• studentType Student1;
• struct studentType
{
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
} tempStudent;
• Defined before the definitions of all of the functions in the
program so that the struct can be used throughout the program.
• Define a struct and also simultaneously declare a struct variable
(as in the preceding statements), then that struct variable
becomes a global variable.
Accessing struct Members
• In arrays: array name together with the relative position (index).
• Access a structure member (component), struct variable name
together with the member name separated by a dot (period).
• dot (.) operator is called the member access operator.
• Syntax:
structVariableName.memberName
• Student1.firstName=“Ali”;
• Student1.testScore=40;
• Student1.GPA=3.5;
struct Operations
int score;
score = (1Student.testScore + Student1.programmingScore) / 2;
student.firstName = newStudent.firstName;
student.lastName = newStudent.lastName;
student.courseGrade = newStudent.courseGrade;
student.testScore = newStudent.testScore;
student.programmingScore = newStudent.programmingScore;
student.GPA = newStudent.GPA;
Comparison
• To compare struct variables, you compare them member-wise.
• No aggregate relational operations are performed on a struct.
if (student == newStudent) //illegal
if (student.firstName == newStudent.firstName &&
student.lastName == newStudent.lastName)
struct I/Os