You are on page 1of 59

|| 1 ||

H
I
P
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
Advanced Constructs
Chapter 3
|| 2 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
What is Covered?
Arrays
Declaring
Accessing/Modifying array elements
Runtime and Compile time initialization of arrays
Multidimensional arrays
Pointers
Declaring
Initializing and using pointer variables
Complex declarations such as pointer to a pointer
Structures
Creating
Accessing structure elements
Aggregate structures
|| 3 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Arrays
A collection of elements of same data type
|| 4 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Declaring arrays
Syntax:
datatype [array_size];
Example:
int numbers[5];
float floatNumbers [10];
|| 5 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Accessing and modifying array values
Syntax:
ArrayName[index_value];
Example:
int numbers[10];
numbers[0] // refers to 1st element in
// numbers array
|| 6 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Memory Allocation
|| 7 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Initializing arrays
Runtime Initialization
Compile Time Initialization
|| 8 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Runtime Initialization
Individual Element Initialization
int numbers[10]; // array declaration
numbers[0] = 10; // array element initialization
numbers[1] = 5;
numbers[2] = 145;

numbers[9] = 24;
Initialization Using a Loop
for (int i=0; i < 10; i++)
numbers[i] = 0;
|| 9 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Compile time Initialization
int numbers[5] = {10, 20, 30, 40, 50};
|| 10 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Partial Initialization
Ex:
int numbers[5] = {10, 20, 30};
Illegal Initialization
int numbers[5] = { ,10, 20, 30};
Omitting Array Size
int numbers[ ] = {10, 20, 30};
|| 11 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example
// program to illustrate use of single dimensional array.
#include "stdafx.h"
#include <iostream>
using namespace std;
void main ()
{
const int NUMBER_OF_STUDENTS=10;
int numbers[NUMBER_OF_STUDENTS];
int Total=0;
|| 12 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example - Continued
for (int i=0; i<NUMBER_OF_STUDENTS; i++)
{
cout << "Enter marks for student ID " << i << " : ";
cin >> numbers[i];
}
for (int j=0; j<NUMBER_OF_STUDENTS; j++)
Total += numbers[j];
cout << "Avg marks for the class : " <<
(float)Total/NUMBER_OF_STUDENTS << endl;
}
|| 13 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Output
Enter marks for student ID 0 : 98
Enter marks for student ID 1 : 56
Enter marks for student ID 2 : 78
Enter marks for student ID 3 : 85
Enter marks for student ID 4 : 95
Enter marks for student ID 5 : 30
Enter marks for student ID 6 : 88
Enter marks for student ID 7 : 85
Enter marks for student ID 8 : 69
Enter marks for student ID 9 : 50
Avg marks for the class : 73.4
|| 14 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Multidimensional arrays
Two-dimensional arrays
n-dimensional arrays
|| 15 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Two-dimensional arrays
Syntax
<DataType> <array_name> [<row>][<col>];
Example:
const int NUMBER_OF_SUBJECTS = 5;
const int NUMBER_OF_STUDENTS = 50;
int marks [NUMBER_OF_SUBJECTS][NUMBER_OF_STUDENTS];
|| 16 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Memory Layout
|| 17 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Initializing two-dimensional arrays
Runtime Initialization
Compile Time Initialization
|| 18 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Runtime Initialization
Syntax:
arrayname [row][col] = data;
Example:
int marks [5][50];
marks [0][5] = 78;
marks [2][10] = 56;
Initialization using loop
for (int row=0; row < MAX_ROWS; row++)
for (int col=0; col < MAX_COLS; col++)
marks [row][col] = 0;
|| 19 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Compile Time Initialization
Example
int marks[5][50] = {
{ 10, 50, 78, },
{ 47, 23, 18, },

};
Other Forms
int marks[5][50] = {10, 50, 78, 85, 23, 11, };
int marks[5][50] =
{
{10, 50, 78},
{47, 23},
};
|| 20 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example
// Program to demonstrate use of multidimensional array
#include "stdafx.h"
#include <iostream>
using namespace std;
void main ()
{
const int NUMBER_OF_STUDENTS = 5;
const int NUMBER_OF_SUBJECTS = 3;
int marks
[NUMBER_OF_STUDENTS][NUMBER_OF_SUBJECTS];
|| 21 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example - Continued
int StudentID =0;
int SubjectNumber = 0;
// Adding data to the array
for (StudentID=0;StudentID<NUMBER_OF_STUDENTS;StudentID++)
{
for ( SubjectNumber=0;
SubjectNumber<NUMBER_OF_SUBJECTS;
SubjectNumber++)
{
marks[StudentID][SubjectNumber]=
100.0* (SubjectNumber+1)/
(StudentID+NUMBER_OF_STUDENTS);
}
}
|| 22 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example - Continued
cout << "\t\t\t";
for ( SubjectNumber=0;
SubjectNumber<NUMBER_OF_SUBJECTS;
SubjectNumber++)
cout << "Subject " << SubjectNumber << "\t";
cout << endl;
for (StudentID=0;StudentID<NUMBER_OF_STUDENTS;StudentID++)
{
cout << "Student " << StudentID << '\t';
for ( SubjectNumber=0;
SubjectNumber<NUMBER_OF_SUBJECTS;
SubjectNumber++)
{
cout << '\t' << marks[StudentID][SubjectNumber]
<< '\t';
}
cout << endl;
}
}
|| 23 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Output
Subject 0 Subject 1 Subject 2
Student 0 20 40 60
Student 1 16 33 50
Student 2 14 28 42
Student 3 12 25 37
Student 4 11 22 33
|| 24 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
n-dimensional Arrays
Syntax:
int x = 5;
int y = 15;
int z = 10;
int Matrix [x][y][z];
|| 25 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Pointers
A pointer variable holds the address of
another variable in the program code
|| 26 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Declaring Pointers
Examples
int *intptr;
float *floatptr;
double *doubleptr;
char *charptr;
|| 27 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Memory Map
int *intptr;
|| 28 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
The addressof Operator
To extract the address of a variable, C++
provides addressof Operator ('&')
The address of intvar is obtained using
syntax &intvar
|| 29 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Assigning using Pointers
Example
int intvar = 100;
int *intptr;
intptr = &intvar;
|| 30 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Memory Map
on Assigning
|| 31 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Accessing variables using pointers
Examples
*intptr = 200;
cout << *intptr;
|| 32 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example
// Program to demonstrate use of pointers
#include "stdafx.h"
#include <iostream>
using namespace std;
void main ()
{
int intVar = 10;
float floatVar = 1.5;
|| 33 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example - Continued
char charVar = 'c';
int *intptr = & intVar;
float *floatptr = &floatVar;
char *charptr = &charVar;
cout << "intVar = " << *intptr << endl;
cout << "floatVar = " << *floatptr << endl;
cout << "charptr = " << *charptr << endl;
cout << endl;
|| 34 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example - Continued
cout << "Modifying variables using pointers ..." << endl
<< endl;
*intptr = 200;
*floatptr = 34.23;
*charptr = 'a';
cout << "Modified variables: " << endl;
cout << "intVar = " << intVar << endl;
cout << "floatVar = " << floatVar << endl;
cout << "charptr = " << charVar << endl;
cout << endl;
}
|| 35 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Output
intVar = 10
floatVar = 1.5
charptr = c
Modifying variables using pointers ...
Modified variables:
intVar = 200
floatVar = 34.23
charptr = a
|| 36 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Printing Pointer Value
Examples:
cout << "Address of intvar : " << (int) intptr << endl;
cout << "Address of intvar : " << (int) &intVar << endl;
Output:
Address of intvar : 0x0012FF70
|| 37 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Address of Pointer Variable
Example
cout << "Address of intptr : " << (int) &intptr << endl;
|| 38 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example
#include "stdafx.h"
#include <iostream>
using namespace std;
void main ()
{
int intVar = 10;
int *intptr = & intVar;
|| 39 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example - Continued
cout << "intVar = " << intVar << endl;
cout << endl;
cout << showbase << hex;
cout << "Address of intVar: " << (int)intptr <<
endl;
cout << "Address of intptr: " << (int)&intptr <<
endl;
cout << endl;
}
|| 40 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Output
intVar = 10
Address of intVar: 0x12ff7c
Address of intptr: 0x12ff78
|| 41 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Memory Map
|| 42 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Pointer to Pointer
Example
char charVar;
char *charptr = &charVar;
char **charptr2 = &charptr;
cout << **charptr2;
|| 43 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example
#include "stdafx.h"
#include <iostream>
using namespace std;
void main ()
{
char charVar = 'A';
char *charptr = &charVar;
char **charptr2 = &charptr;
cout << "Accessing charVar directly: " <<
charVar << endl;
cout << "Accessing charVar through first pointer: " <<
*charptr << endl;
|| 44 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example - Continued
cout << "Accessing charVar through double pointer: " <<
**charptr2 << endl;
cout << endl;
cout << showbase << hex;
cout << "Address of charVar: " <<
(int)&charVar << endl;
cout << "Address of charptr: " <<
(int)&charptr << endl;
cout << "Address of charptr2: " << (int)&charptr2 << endl;
}
|| 45 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Output
Accessing charVar directly: A
Accessing charVar through first pointer: A
Accessing charVar through double pointer: A
Address of charVar: 0x12ff7c
Address of charptr: 0x12ff78
Address of charptr2: 0x12ff74
|| 46 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Memory Map (Double Pointer)
|| 47 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Structures
Syntax
struct structure_name
{
data_type element1;
data_type element2;
data_type element3;
} object_name;
|| 48 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Structure Example
struct Employee
{
char name[50];
int age;
char gender;
double basic_salary;
} FullTimeEmp, PartTimeEmp;
|| 49 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Accessing Structure Elements
Using dot operator
Using pointers
|| 50 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Accessing Using dot (.) Operator
Syntax:
<structure_name>.<field_name>
Example
FullTimeEmp.name = "Santosh";
FullTimeEmp.basic_salary = 1000;
cout << "Name: " << FullTimeEmp.name;
cout << "Salary: " << FullTimeEmp.basic_salary;
|| 51 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example
// Program to demonstrate use of structures
#include "stdafx.h"
#include <iostream>
using namespace std;
struct Employee {
char name [50];
int age;
char gender;
double basic_salary;
|| 52 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example - Continued
} emp1, emp2;
void printEmployeeDetails (Employee emp);
void main ()
{
cout << "Initializing Employee structure variables ..."
<< endl << endl;
strcpy ( emp1.name, "Santosh");
emp1.age = 35;
emp1.gender = 'M';
emp1.basic_salary = 20000;
strcpy ( emp2.name,"Rahul");
emp2.age = 28;
|| 53 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example - Continued
emp2.gender = 'M';
emp2.basic_salary = 15000;
cout << "Printing Employee details:\n";
printEmployeeDetails (emp1);
printEmployeeDetails (emp2);
}
|| 54 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Example - Continued
void printEmployeeDetails (Employee emp)
{
cout << endl;
cout << "Name: " << emp.name << endl;
cout << "Age: " << emp.age << endl;
if (emp.gender == 'M')
cout << "Gender: Male" << endl;
else
cout << "Gender: Female" << endl;
cout << "Salary: Rs." <<emp.basic_salary << endl;
}
|| 55 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Program Output
Initializing Employee structure variables ...
Printing Employee details:
Name: Santosh
Age: 35
Gender: Male
Salary: Rs.20000
Name: Rahul
Age: 28
Gender: Male
Salary: Rs.15000
|| 56 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Accessing Using Pointer
Example
Employee *ptrEmp;
ptrEmp = &emp1;
ptrEmp -> name;
ptrEmp -> basic_salary;
|| 57 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Structures Containing Other Structure
Data Types
Example
struct Company {
char name[50];
Employee FullTimeEmp;
Employee PartTimeEmp;
}ABCOM;
Accessing Elements
ABCOM.FullTimeEmp.name
Company *ptrCompany = &ABCOM;
ptrCompany->FullTimeEmp.age = 50;
|| 58 ||
C
+
+
C
+
+
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
H
I
P
Summary
What you learned?
Arrays
Declaring
Accessing/Modifying array elements
Runtime and Compile time initialization of arrays
Multidimensional arrays
Pointers
Declaring
Initializing and using pointer variables
Complex declarations such as pointer to a pointer
Structures
Creating
Accessing structure elements
Aggregate structures
|| 59 ||
H
I
P
Copyright @2006, Prof Poornachandra Sarang, Ph.D.
Email ProfSarang@gmail.com
H
I
P
Conclusion

You might also like