SlideShare a Scribd company logo
UNIT 3
Pointer and Arrays
26/09/2015 Jitendra R. Patil 1
Pointers.
//Demonstration.
#include<conio.h>
#include<iostream.h>
void main()
{
int x ;
float y;
x = 10;
y = 20.0;
cout<<“Value’s of x and y = “<<x<<y;
getch();
}
26/09/2015 Jitendra R. Patil 2
4001 4002 4003
4004 4005 4006
4007 4008 4009
Memory
Every Block is
a memory cell
Cell no’s / Address
10
20.0
4007
x
4007
4002
y
Pointers
• A pointer is a variable that holds a memory address, usually
the location of another variable in memory
• The contents of (*) is used to declare a pointer
• Address-of operator (&) :
The & operator can find address occupied by a variable.
26/09/2015 Jitendra R. Patil 3
Example : Address-of Operator
#include <iostream>
using namespace std;
int main()
{
int var1 = 3;
int var2 = 24;
int var3 = 17;
cout<<&var1<<endl;
cout<<&var2<<endl;
cout<<&var3<<endl;
return 0;
}26/09/2015 Jitendra R. Patil 4
Pointer Declaration :
Syntax:
Data_type
* is used in declaration statements, it denotes a pointer
Defining a Pointer Variable
int *iptr;
iptr can hold the address of an int
26/09/2015 Jitendra R. Patil 5
Pointer Variables Assignment:
int num = 25;
int *iptr;
iptr = &num; //places the memory address of num into iptr
cout << iptr; // prints 0x4a00
cout << *itptr; // prints 25
26/09/2015 Jitendra R. Patil 6
Pointer Operator’s.
void main()
{ int *int_ptr;
float *float_ptr;
int x = 10 ;
float y = 20.0;
cout<<“Value’s of x and y = “<<x<<“ “<<y;
int_ptr = &x;
float_ptr = &y;
cout<<"int_ptr = "<<int_ptr;
cout<<"n float_ptr = "<<float_ptr;
cout<<"n value in int_pointer = "<< *int_ptr;
cout<<"n value in float_ptr = "<< *float_ptr;
getch();
}26/09/2015 Jitendra R. Patil 7
20.0
4002
10
4007
x y
Value at Address.
Address
Name of address
Pointers contd.
26/09/2015 Jitendra R. Patil 8
& Address of (Reference) Operator.
* Value At Address (Indirection) operator.
// Demonstration.
#include <iostream.h>
#include<conio.h>
void main ()
{
int variable ;
int *mypointer;
mypointer = &variable;
*mypointer = 50;
cout << "value in variable is " <<variable<<endl;
getch();
}
Address of variable = mypointer
50 = value at address
Value in variable is 50
50
3722
variable
Demo working of pointers
#include<iostream.h>
Void main()
{
int marks=70; //marks is an integer
int *p // p is a pointer
p=&marks; // p points to marks
cout<<“n Address is : ”<<p;
cout<<“n Value at that address is : ”<<*p;
}
26/09/2015 Jitendra R. Patil 9
void pointer
• In C++ General Purpose Pointer is called as void
Pointer.
• It does not have any data type associated with it
• It can store address of any type of variable
• Syntax
void *pointer_name
26/09/2015 Jitendra R. Patil 10
#include<iostream.h>
int main()
{
int i;
char c;
void *the_data;
i = 6;
c = 'a';
the_data = &i;
printf("the_data points to the integer value %dn", *the_data);
the_data = &c;
printf("the_data now points to the character %cn", *the_data);
return 0; }26/09/2015 Jitendra R. Patil 11
This Pointer
#include<iostream.h>
#include<conio.h>
class test
{
private:
int num;
public:
void get_val(num)
{
this->num=num;
}
26/09/2015 Jitendra R. Patil 12
Void print_val()
{
Cout<<“n The Value is”<<num;
}
};
void main()
{
test obj;
int num;
clrscr();
cout<<“”n Enter some Value : “;
cin>>num;
obj.get_val(num);
obj.print();
getch();
}
26/09/2015 Jitendra R. Patil 13
#include<iostream>
using namespace std;
/* local variable is same as a member's name */
class Test
{
private:
int x;
public:
void setX (int x)
{
// The 'this' pointer is used to retrieve the object's x
// hidden by the local variable 'x'
this->x = x;
}
void print()
{
cout << "x = " << x << endl;
}
};
26/09/2015 Jitendra R. Patil 14
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
Pointer to object
#include<iostream.h>
#include<conio.h>
Class test
{
int a;
public:
test(int b)
{
a=b;
}
26/09/2015 Jitendra R. Patil 15
int get_val()
{
return a;
}
};
int main()
{
Test obj(100),*ptr_obj;
ptr_obj=&obj; //address of object is stored in pointer variable
cout<<“value obtained using pointer to object is…”<<endl;
cout<<ptr_obj->get_val()<<endl;
return 0;
}
26/09/2015 Jitendra R. Patil 16
Array
• Array is derived data type
• An array is a sequence of data item of homogeneous value(same
type).
• It is collection of variable of same type
• Specific element in array is accessed by an index
• All array consist of continuous memory locations
26/09/2015 Jitendra R. Patil 17
• Syntax: data_type array_name[size];
• int age[5]
26/09/2015 Jitendra R. Patil 18
Array Index
Elements of Array
Advantages
• Arrays can store a large number of value with single name.
• Arrays are used to process many value easily and quickly.
• The values stored in an array can be sorted easily.
• The search process can be applied on arrays easily.
26/09/2015 Jitendra R. Patil 19
#include <iostream.h>
void main()
{
int n[5];
cout<<"Enter 5 numbers: ";
for (int i = 0; i < 5; ++i)
{
cin>>n[i];
}
cout<<"First number: "<<n[0]<<endl;
cout<<"Last number: "<<n[4]<<endl;
}
26/09/2015 Jitendra R. Patil 20
Types of Arrays:
• One-Dimensional Array
• Two-Dimensional Array
• Multi-Dimensional Array
26/09/2015 Jitendra R. Patil 21
One-Dimensional Array
A type of array in which all elements are arranged in the form of a list is
known as 1-D array or single dimensional array or linear list.
Declaring 1-D Array:
data_type Variable[length]; e.g: int marks[5];
oData _type: Data type of values to be stored in the array.
oVariable: Name of the array.
oLength: Number of elements.
int marks 0 1 2 3 4
26/09/2015 Jitendra R. Patil 22
Array Initialization
• Size can be set from initial value list
int balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
• No default value
float pressure[10];
• Missing initialization values use zero
float pressure[10] = {2.101, 2.32, 1.44}
26/09/2015 Jitendra R. Patil 23
26/09/2015 24
Accessing One Dimensional Array Elements
#include<iostream>
#include<conio.h>
Void main()
{
int a[10];
clrscr();
cout<<“enter the array”;
for (i=0;i<10;i++)
{
cin>>a[i];
}
cout<<“the entered array is”;
for(i=0;i<10;i++)
{
cout<<“Array = ”<<a[i]);
}
getch();
}
Array declaration
Taking values from user
Printing the values
OUTPUT
Enter the array
1 2 3 4 5 6 7 8 9 10
Entered array is
1
2
3
4
5
6
7
8
9
10
Jitendra R. Patil
Two-Dimensional Array
• Two-D array can be considered as table that consists of rows and columns.
• Each element in 2-D array is referred with the help of two indexes. One index
indicates row and second indicates the column.
Declaring 2-D Array:
Data_type Variable[row][column];
e.g: int arr[4][3];
o Data _type: Data type of values to be stored in the array. e.g: int arr[4][3];
o Variable: Name of the array.
o Rows : # of Rows in the table of array.
o Column : # of Columns in the table of array.
26/09/2015 Jitendra R. Patil 25
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
int a[10][10];
int b[10][10];
int x, y, i, j;
cout<<"n Enter the number of rows and columns :::nn";
cin>>x>>y;
cout<<"nn Enter elements for Matrix A :::nn";
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cin>>a[i][j];
}
cout<<"n";
}26/09/2015 Jitendra R. Patil 26
Accessing Two Dimensional Array Elements
cout<<"nnEnter elements for Matrix B :::nn";
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cin>>b[i][j];
}
cout<<"n";
}
cout<<"nnMatrix A :nn";
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cout<<"t"<<a[i][j];
}
cout<<"nn";
}
26/09/2015 Jitendra R. Patil 27
cout<<"nnMatrix B :nn";
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cout<<"t"<<b[i][j];
}
cout<<"nn";
}
26/09/2015 Jitendra R. Patil 28
cout<<"nnAddition of Matrix A and Matrix B :nn";
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cout<<"t"<<a[i][j]+b[i][j];
}
cout<<"nn";
}
getch();
return 0;
}
26/09/2015 Jitendra R. Patil 29
Array of Objects
26/09/2015 Jitendra R. Patil 30
#include<iostream.h>
#include<conio.h>
class rec
{
private:
int l, b;
public:
rec(int a, int c)
{
l=a; 09/09/2015
b=c;
}
void put()
{
cout<<"Area is : "<<l*b <<endl;
}
};
void main()
{
clrscr();
rec obj[3]={rec(3,6),rec(2,5),rec(5,5)};
cout<<"Displaying Areas of Rectangles :
n";
for(int i=0;i<3;i++)
obj[i].put();
getch();
}
#include<conio.h>
#include<string.h>
#include<iostream.h>
class student
{ private:
int s1,s2,s3,s4,s5,total;
char name[20],grade[10];
float per;
public:
int roll_no;
void input()
{
cout<<"n Enter the Name-";
cin>>name;
cout<<"n Enter the Roll No-";
cin>>roll_no;
cout<<"n M3 :";
cin>>s1;
cout<<"n OOT :";
cin>>s2;
cout<<"n ADE :";
cin>>s3;
cout<<"n MPMC :";
cin>>s4;
cout<<"n DSGT :";
cin>>s5;
cout<<"n";
total= (s1+s2+s3+s4+s5);
cout<<"n Total = "<<total;
per= total/5;
cout<<"n Percentage is " <<per;
}
26/09/2015 Jitendra R. Patil 31
void output()
{
if(per>=70)
strcpy(grade, "Distinction");
else if(per>=60)
strcpy(grade, "First Class");
else if(per>=40)
strcpy(grade,"Second class");
else if(per<40)
strcpy(grade, "Fail");
cout<<"nn";
cout<<"nt******MARKSHEET******";
cout<<"n";
cout<<"n Name :"<<name;
cout<<"n Roll No. : "<<roll_no;
cout<<"n M3 :"<<s1;
cout<<"n OOT :"<<s2;
cout<<"n ADE :"<<s3;
cout<<"n MPMC :"<<s4;
cout<<"n DSGT :"<<s5;
cout<<"n Total :"<<total;
cout<<"n Percentage :"<<per<<"%";
cout<<"n Grade :"<<grade;
}
};
26/09/2015 Jitendra R. Patil 32
void main()
{
int n,r,i;
student s[5];
cout<<"nEnter how many records u want to
insert:";
cin>>n;
for(i=0;i<n;i++)
s[i].input();
cout<<"n";
cout<<"nEnter the roll no for marksheet:-";
cin>>r;
for(i=0;i<n;i++)
{
if(s[i].roll_no==r)
{
s[i].output();
}
else
{
cout<<"n File doesnt Exit";
}
}
getch();
}
26/09/2015 Jitendra R. Patil 33
Dynamic Memory
• Memory in C++ program is divided into two parts
• The stack: All variables declared inside the function will take up
memory from the stack.
• The heap: This is unused memory of the program and can be used to
allocate the memory dynamically when program runs.
26/09/2015 Jitendra R. Patil 34
Memory Management Operator
• new operator
To allocate memory space using dynamically during runtime we can
use new operator
Syntax :
pointer_variable = new datatype
• delete operator
Free the memory space which is allocated with ‘ new’ we can use
delete operators
Syntax :
delete pointer_variable
26/09/2015 Jitendra R. Patil 35

More Related Content

What's hot

重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)
Chris Huang
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
rohassanie
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit Testing
Kevlin Henney
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
rohassanie
 
What\'s New in C# 4.0
What\'s New in C# 4.0What\'s New in C# 4.0
What\'s New in C# 4.0
Eyal Vardi
 
Java8 stream
Java8 streamJava8 stream
Java8 stream
koji lin
 
Computer Programming- Lecture 8
Computer Programming- Lecture 8Computer Programming- Lecture 8
Computer Programming- Lecture 8
Dr. Md. Shohel Sayeed
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design Patterns
Stefano Fago
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
FALLEE31188
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Data
greenwop
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
Nalinee Choudhary
 
Functional C++
Functional C++Functional C++
Functional C++
Kevlin Henney
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
Jay Patel
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
Kumar
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
vandna123
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
Kevlin Henney
 
Building l10n Payroll Structures from the Ground up
Building l10n Payroll Structures from the Ground upBuilding l10n Payroll Structures from the Ground up
Building l10n Payroll Structures from the Ground up
Odoo
 
Linq Sanjay Vyas
Linq   Sanjay VyasLinq   Sanjay Vyas
Linq Sanjay Vyas
rsnarayanan
 
Functional solid
Functional solidFunctional solid
Functional solid
Matt Stine
 

What's hot (20)

重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit Testing
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
What\'s New in C# 4.0
What\'s New in C# 4.0What\'s New in C# 4.0
What\'s New in C# 4.0
 
Java8 stream
Java8 streamJava8 stream
Java8 stream
 
Computer Programming- Lecture 8
Computer Programming- Lecture 8Computer Programming- Lecture 8
Computer Programming- Lecture 8
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design Patterns
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Data
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 
Functional C++
Functional C++Functional C++
Functional C++
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
Building l10n Payroll Structures from the Ground up
Building l10n Payroll Structures from the Ground upBuilding l10n Payroll Structures from the Ground up
Building l10n Payroll Structures from the Ground up
 
Linq Sanjay Vyas
Linq   Sanjay VyasLinq   Sanjay Vyas
Linq Sanjay Vyas
 
Functional solid
Functional solidFunctional solid
Functional solid
 

Similar to Unit 3

Pointer
PointerPointer
Pointer
Fahuda E
 
Pointers
PointersPointers
Pointers
rajshreemuthiah
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptx
happycocoman
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
Pointers in Programming
Pointers in ProgrammingPointers in Programming
Pointers in Programming
HamadZia1
 
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure Programming
Marian Marinov
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
Mohammed Sikander
 
pointers
pointerspointers
pointers
teach4uin
 
Pointers
PointersPointers
Pointers
sanya6900
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Meghaj Mallick
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]
Abdullah khawar
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
HIMANSUKUMAR12
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
Pointers
PointersPointers
Pointers
Hitesh Wagle
 
P1
P1P1
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
Chaand Sheikh
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
minal kumar soni
 
Introduction to cpp (c++)
Introduction to cpp (c++)Introduction to cpp (c++)
Introduction to cpp (c++)
Arun Umrao
 
C++ examples &revisions
C++ examples &revisionsC++ examples &revisions
C++ examples &revisions
Ibrahim El-Torbany
 
Arrays
ArraysArrays

Similar to Unit 3 (20)

Pointer
PointerPointer
Pointer
 
Pointers
PointersPointers
Pointers
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptx
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Pointers in Programming
Pointers in ProgrammingPointers in Programming
Pointers in Programming
 
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure Programming
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
pointers
pointerspointers
pointers
 
Pointers
PointersPointers
Pointers
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Pointers
PointersPointers
Pointers
 
P1
P1P1
P1
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Introduction to cpp (c++)
Introduction to cpp (c++)Introduction to cpp (c++)
Introduction to cpp (c++)
 
C++ examples &revisions
C++ examples &revisionsC++ examples &revisions
C++ examples &revisions
 
Arrays
ArraysArrays
Arrays
 

More from S.S.B.T’s. College of Engineering & Technology

Unit 5
Unit 5Unit 5
Unit 4
Unit 4Unit 4
Unit 2
Unit 2Unit 2
Unit 1
Unit 1Unit 1
Unit 5
Unit 5Unit 5
Unit 4
Unit 4Unit 4
Unit 3
Unit 3Unit 3
Unit 2
Unit 2Unit 2
Unit 1
Unit 1Unit 1

More from S.S.B.T’s. College of Engineering & Technology (9)

Unit 5
Unit 5Unit 5
Unit 5
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit 1
Unit 1Unit 1
Unit 1
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit 1
Unit 1Unit 1
Unit 1
 

Recently uploaded

Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
Madan Karki
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
PKavitha10
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
Data Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptxData Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptx
ramrag33
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
SakkaravarthiShanmug
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 

Recently uploaded (20)

Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
Data Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptxData Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptx
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 

Unit 3

  • 1. UNIT 3 Pointer and Arrays 26/09/2015 Jitendra R. Patil 1
  • 2. Pointers. //Demonstration. #include<conio.h> #include<iostream.h> void main() { int x ; float y; x = 10; y = 20.0; cout<<“Value’s of x and y = “<<x<<y; getch(); } 26/09/2015 Jitendra R. Patil 2 4001 4002 4003 4004 4005 4006 4007 4008 4009 Memory Every Block is a memory cell Cell no’s / Address 10 20.0 4007 x 4007 4002 y
  • 3. Pointers • A pointer is a variable that holds a memory address, usually the location of another variable in memory • The contents of (*) is used to declare a pointer • Address-of operator (&) : The & operator can find address occupied by a variable. 26/09/2015 Jitendra R. Patil 3
  • 4. Example : Address-of Operator #include <iostream> using namespace std; int main() { int var1 = 3; int var2 = 24; int var3 = 17; cout<<&var1<<endl; cout<<&var2<<endl; cout<<&var3<<endl; return 0; }26/09/2015 Jitendra R. Patil 4
  • 5. Pointer Declaration : Syntax: Data_type * is used in declaration statements, it denotes a pointer Defining a Pointer Variable int *iptr; iptr can hold the address of an int 26/09/2015 Jitendra R. Patil 5
  • 6. Pointer Variables Assignment: int num = 25; int *iptr; iptr = &num; //places the memory address of num into iptr cout << iptr; // prints 0x4a00 cout << *itptr; // prints 25 26/09/2015 Jitendra R. Patil 6
  • 7. Pointer Operator’s. void main() { int *int_ptr; float *float_ptr; int x = 10 ; float y = 20.0; cout<<“Value’s of x and y = “<<x<<“ “<<y; int_ptr = &x; float_ptr = &y; cout<<"int_ptr = "<<int_ptr; cout<<"n float_ptr = "<<float_ptr; cout<<"n value in int_pointer = "<< *int_ptr; cout<<"n value in float_ptr = "<< *float_ptr; getch(); }26/09/2015 Jitendra R. Patil 7 20.0 4002 10 4007 x y Value at Address. Address Name of address
  • 8. Pointers contd. 26/09/2015 Jitendra R. Patil 8 & Address of (Reference) Operator. * Value At Address (Indirection) operator. // Demonstration. #include <iostream.h> #include<conio.h> void main () { int variable ; int *mypointer; mypointer = &variable; *mypointer = 50; cout << "value in variable is " <<variable<<endl; getch(); } Address of variable = mypointer 50 = value at address Value in variable is 50 50 3722 variable
  • 9. Demo working of pointers #include<iostream.h> Void main() { int marks=70; //marks is an integer int *p // p is a pointer p=&marks; // p points to marks cout<<“n Address is : ”<<p; cout<<“n Value at that address is : ”<<*p; } 26/09/2015 Jitendra R. Patil 9
  • 10. void pointer • In C++ General Purpose Pointer is called as void Pointer. • It does not have any data type associated with it • It can store address of any type of variable • Syntax void *pointer_name 26/09/2015 Jitendra R. Patil 10
  • 11. #include<iostream.h> int main() { int i; char c; void *the_data; i = 6; c = 'a'; the_data = &i; printf("the_data points to the integer value %dn", *the_data); the_data = &c; printf("the_data now points to the character %cn", *the_data); return 0; }26/09/2015 Jitendra R. Patil 11
  • 12. This Pointer #include<iostream.h> #include<conio.h> class test { private: int num; public: void get_val(num) { this->num=num; } 26/09/2015 Jitendra R. Patil 12
  • 13. Void print_val() { Cout<<“n The Value is”<<num; } }; void main() { test obj; int num; clrscr(); cout<<“”n Enter some Value : “; cin>>num; obj.get_val(num); obj.print(); getch(); } 26/09/2015 Jitendra R. Patil 13
  • 14. #include<iostream> using namespace std; /* local variable is same as a member's name */ class Test { private: int x; public: void setX (int x) { // The 'this' pointer is used to retrieve the object's x // hidden by the local variable 'x' this->x = x; } void print() { cout << "x = " << x << endl; } }; 26/09/2015 Jitendra R. Patil 14 int main() { Test obj; int x = 20; obj.setX(x); obj.print(); return 0; }
  • 15. Pointer to object #include<iostream.h> #include<conio.h> Class test { int a; public: test(int b) { a=b; } 26/09/2015 Jitendra R. Patil 15
  • 16. int get_val() { return a; } }; int main() { Test obj(100),*ptr_obj; ptr_obj=&obj; //address of object is stored in pointer variable cout<<“value obtained using pointer to object is…”<<endl; cout<<ptr_obj->get_val()<<endl; return 0; } 26/09/2015 Jitendra R. Patil 16
  • 17. Array • Array is derived data type • An array is a sequence of data item of homogeneous value(same type). • It is collection of variable of same type • Specific element in array is accessed by an index • All array consist of continuous memory locations 26/09/2015 Jitendra R. Patil 17
  • 18. • Syntax: data_type array_name[size]; • int age[5] 26/09/2015 Jitendra R. Patil 18 Array Index Elements of Array
  • 19. Advantages • Arrays can store a large number of value with single name. • Arrays are used to process many value easily and quickly. • The values stored in an array can be sorted easily. • The search process can be applied on arrays easily. 26/09/2015 Jitendra R. Patil 19
  • 20. #include <iostream.h> void main() { int n[5]; cout<<"Enter 5 numbers: "; for (int i = 0; i < 5; ++i) { cin>>n[i]; } cout<<"First number: "<<n[0]<<endl; cout<<"Last number: "<<n[4]<<endl; } 26/09/2015 Jitendra R. Patil 20
  • 21. Types of Arrays: • One-Dimensional Array • Two-Dimensional Array • Multi-Dimensional Array 26/09/2015 Jitendra R. Patil 21
  • 22. One-Dimensional Array A type of array in which all elements are arranged in the form of a list is known as 1-D array or single dimensional array or linear list. Declaring 1-D Array: data_type Variable[length]; e.g: int marks[5]; oData _type: Data type of values to be stored in the array. oVariable: Name of the array. oLength: Number of elements. int marks 0 1 2 3 4 26/09/2015 Jitendra R. Patil 22
  • 23. Array Initialization • Size can be set from initial value list int balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}; • No default value float pressure[10]; • Missing initialization values use zero float pressure[10] = {2.101, 2.32, 1.44} 26/09/2015 Jitendra R. Patil 23
  • 24. 26/09/2015 24 Accessing One Dimensional Array Elements #include<iostream> #include<conio.h> Void main() { int a[10]; clrscr(); cout<<“enter the array”; for (i=0;i<10;i++) { cin>>a[i]; } cout<<“the entered array is”; for(i=0;i<10;i++) { cout<<“Array = ”<<a[i]); } getch(); } Array declaration Taking values from user Printing the values OUTPUT Enter the array 1 2 3 4 5 6 7 8 9 10 Entered array is 1 2 3 4 5 6 7 8 9 10 Jitendra R. Patil
  • 25. Two-Dimensional Array • Two-D array can be considered as table that consists of rows and columns. • Each element in 2-D array is referred with the help of two indexes. One index indicates row and second indicates the column. Declaring 2-D Array: Data_type Variable[row][column]; e.g: int arr[4][3]; o Data _type: Data type of values to be stored in the array. e.g: int arr[4][3]; o Variable: Name of the array. o Rows : # of Rows in the table of array. o Column : # of Columns in the table of array. 26/09/2015 Jitendra R. Patil 25
  • 26. #include<iostream.h> #include<conio.h> int main() { clrscr(); int a[10][10]; int b[10][10]; int x, y, i, j; cout<<"n Enter the number of rows and columns :::nn"; cin>>x>>y; cout<<"nn Enter elements for Matrix A :::nn"; for(i=0;i<x;i++) { for(j=0;j<y;j++) { cin>>a[i][j]; } cout<<"n"; }26/09/2015 Jitendra R. Patil 26 Accessing Two Dimensional Array Elements
  • 27. cout<<"nnEnter elements for Matrix B :::nn"; for(i=0;i<x;i++) { for(j=0;j<y;j++) { cin>>b[i][j]; } cout<<"n"; } cout<<"nnMatrix A :nn"; for(i=0;i<x;i++) { for(j=0;j<y;j++) { cout<<"t"<<a[i][j]; } cout<<"nn"; } 26/09/2015 Jitendra R. Patil 27
  • 29. cout<<"nnAddition of Matrix A and Matrix B :nn"; for(i=0;i<x;i++) { for(j=0;j<y;j++) { cout<<"t"<<a[i][j]+b[i][j]; } cout<<"nn"; } getch(); return 0; } 26/09/2015 Jitendra R. Patil 29
  • 30. Array of Objects 26/09/2015 Jitendra R. Patil 30 #include<iostream.h> #include<conio.h> class rec { private: int l, b; public: rec(int a, int c) { l=a; 09/09/2015 b=c; } void put() { cout<<"Area is : "<<l*b <<endl; } }; void main() { clrscr(); rec obj[3]={rec(3,6),rec(2,5),rec(5,5)}; cout<<"Displaying Areas of Rectangles : n"; for(int i=0;i<3;i++) obj[i].put(); getch(); }
  • 31. #include<conio.h> #include<string.h> #include<iostream.h> class student { private: int s1,s2,s3,s4,s5,total; char name[20],grade[10]; float per; public: int roll_no; void input() { cout<<"n Enter the Name-"; cin>>name; cout<<"n Enter the Roll No-"; cin>>roll_no; cout<<"n M3 :"; cin>>s1; cout<<"n OOT :"; cin>>s2; cout<<"n ADE :"; cin>>s3; cout<<"n MPMC :"; cin>>s4; cout<<"n DSGT :"; cin>>s5; cout<<"n"; total= (s1+s2+s3+s4+s5); cout<<"n Total = "<<total; per= total/5; cout<<"n Percentage is " <<per; } 26/09/2015 Jitendra R. Patil 31
  • 32. void output() { if(per>=70) strcpy(grade, "Distinction"); else if(per>=60) strcpy(grade, "First Class"); else if(per>=40) strcpy(grade,"Second class"); else if(per<40) strcpy(grade, "Fail"); cout<<"nn"; cout<<"nt******MARKSHEET******"; cout<<"n"; cout<<"n Name :"<<name; cout<<"n Roll No. : "<<roll_no; cout<<"n M3 :"<<s1; cout<<"n OOT :"<<s2; cout<<"n ADE :"<<s3; cout<<"n MPMC :"<<s4; cout<<"n DSGT :"<<s5; cout<<"n Total :"<<total; cout<<"n Percentage :"<<per<<"%"; cout<<"n Grade :"<<grade; } }; 26/09/2015 Jitendra R. Patil 32
  • 33. void main() { int n,r,i; student s[5]; cout<<"nEnter how many records u want to insert:"; cin>>n; for(i=0;i<n;i++) s[i].input(); cout<<"n"; cout<<"nEnter the roll no for marksheet:-"; cin>>r; for(i=0;i<n;i++) { if(s[i].roll_no==r) { s[i].output(); } else { cout<<"n File doesnt Exit"; } } getch(); } 26/09/2015 Jitendra R. Patil 33
  • 34. Dynamic Memory • Memory in C++ program is divided into two parts • The stack: All variables declared inside the function will take up memory from the stack. • The heap: This is unused memory of the program and can be used to allocate the memory dynamically when program runs. 26/09/2015 Jitendra R. Patil 34
  • 35. Memory Management Operator • new operator To allocate memory space using dynamically during runtime we can use new operator Syntax : pointer_variable = new datatype • delete operator Free the memory space which is allocated with ‘ new’ we can use delete operators Syntax : delete pointer_variable 26/09/2015 Jitendra R. Patil 35