You are on page 1of 28

Lecture 03: Pointers & Structs

109104 – Object Oriented Programming (OOP)

Engr. Muhammad Asad

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.

Syntex: sizeof variableName ;


sizeof(variableName);
Size of Variables
• The size of data types can vary among system.
Size of Arrays
• Size of array= size of data type* length of array
• Syntex: sizeof(arrayName);

Length of array= sizeof(arrayName)/ sizeof(arrayName[0]);


Size of Pointer
• 4 bytes
• If the system is 16-bit, size of void pointer is 2 bytes. If the system is
32-bit, size of void pointer is 4 bytes. If the system is 64-bit, size of
void pointer is 8 bytes.
• When used with pointers, it returns size of the pointer i.e. 4bytes not
the size of variable it is pointing to.
Syntex: int *ptr;
sizeof ptr;
Pointer Arithmetic

• Depends on the size of the data it is pointing to, so its machine


dependent.
• C++ allows the use of pointers as operands.
• Various operations can be performed on pointers.
• Appropriate only for built-in array elements.
• Addition (+=), subtraction (-=), increment (++) and decrement (--).
• Pointer of same type can also be added and subtracted. This
operation to be performed on elements of the same array.
Change in pointer address after Arithmetic
• Addition: (ptr+=2);
• Initial value of ptr =200;
New value= initial value + jump*data type of value
=200+2*4;
=208
Void Pointers

• Can point to any type of objects.


• Cant be dereference as it does not know what type of data it is
pointing to and it doesn’t know the size so compile time error.
• Not associated with any data type
• Also called general purpose pointer.
• Points to memory location in storage.
• Memory allocating functions usually return void pointers.
• Size of void pointer= 4 bytes.
• Void pointer cant be dereferenced.  
• To dereference a pointer compiler must know data type and size
which is unknown incase of void pointer.
• A void pointer must be explicitly cast into another type
of pointer to be dereferenced otherwise compilation error.
• Allowed Operations are comparing void pointers, casting other
pointer to void pointers and assigning addresses.
• All other operations are compilation errors.
Pointer based Strings

• String is character array.


• Pointer based string is an array of characters ending with a null
character.
• Pointer to the first character of the string.
• Character Constants: Integer value of characters in ASCII, represented in
single quotes.
• Sizeof of string determines the size of characters in string and null
character.
• Char color[]=“blue”;
• Const char *colorptr=“blue”;
• Reading string into built in arrays using cin
• Terminated by white space or end of line character.
Cin>> variableName;
• Size of the word can also be specified using setw()
cin>>setw(20)>>VariableName;
• Means a string of 19 characters should be given in input.
Char s[20];
Cin.getline(s, 20, ‘\n’);
• Read characters from keyboard
• End of line ends the string.
struct
• User defined structured data type
• Also called Record
• Heterogeneous data structure
• Keyword: struct
• Group items of different data types
• struct: A collection of a fixed number of components in which the
components are accessed by name. The components may be of
different types.
• Components are called members of struct.
Definition:

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;

if (score >= 90)


newStudent.courseGrade = 'A';
else if (score >= 80)
newStudent.courseGrade = 'B';
else if (score >= 70)
newStudent.courseGrade = 'C';
else if (score >= 60)
newStudent.courseGrade = 'D';
else
newStudent.courseGrade = 'F';
struct Assignment
student = newStudent;

is equivalent to the following statements:

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

• No aggregate input/output operations are allowed on a struct


variable.
• Data in a struct variable must be read one member at a time.

cout << newStudent.firstName;


cin >> newStudent.firstName;
cin >> newStudent.testScore >> newStudent.programmingScore;
Struct & Functions
• A function cannot return a value of type array.
• A struct variable can be passed as a parameter either by value or by
reference.
• A function can return a value of type struct.
void printStudent(studentType student)
{
cout << student.firstName << " " << student.lastName
<< " " << student.courseGrade
<< " " << student.testScore
<< " " << student.programmingScore
<< " " << student.GPA << endl;
}
void readIn(studentType& student)
{
int score;
cin >> student.firstName >> student.lastName;
cin >> student.testScore >> student.programmingScore;
cin >> student.GPA;
}
The statement:
readIn(newStudent);
Home Assignment
• Try all these codes at home

You might also like