SlideShare a Scribd company logo
CHAPTER 4:
ARRAY, STRUCTURE AND POINTER
• An array is a collection of data
storage locations, which holds the
same type of data. Each storage
location is called an element of the
array
2
Introduction To Array
• An array is a structured collection of components (often called
array elements) that can be accessed individually by specifying
the position of a component with a single index value.
• General form to declare an array
Data_type Array_name [ Array_size];
– Data_type is the specifier that indicates what data type .
– Array_name is the name of the declared array.
– Array_size defines how many elements the array can contain
3
Array Declarations
float price[10];
float specifies as the data type
price is the name of the declared array
the size of the array is 10
char name [50];
int age [20];
4
Example:
• int x[6]={ 1, 13, 33, 7, 20, 6};
– The integer inside the braces { and } are
assigned to the corresponding elements of
the array.
5
Initializing An Array
1 13 33 7 20 6
6
Illustrate one dimensional array
int x[6]={ 1, 13, 33, 7, 20, 6};
x[0] x[1] x[2] x[3] x[4] x[5]
index
Element 2 Element 3 Element 4 Element 5 Element 6Element 1
int x [9] = {0,3,7,3,0,20,61,17,800 };
double x [8] = {1.2, 1.4, 1.7, 2.6, 3.4, 2.6,
1.2, 3.1};
char x[9] = {‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’};
7
Example:
# include <iostream>
using namespace std;
int main()
{
float price[3];
cout <<“price 1:";
cin>>price[0];
cout <<“price 2:";
cin>>price[1];
cout <<“price 3:";
cin>>price[2];
cout<<“TOTAL PRICE:”<<price[0]+price[1]+price[2];
}
return 0;
}
8
Example of program
# include <iostream>
using namespace std;
int main()
{
float price[3];
int i;
total=0;
for( i=0;i<3;i++)
{
cout <<“price :“<<0+i<<;
cin>>price[i];
total=total+price[i];
}
cout<<“TOTAL PRICE:”<<total;
return 0;
}
9
Example of program
# include <iostream>
using namespace std;
main()
{
int a;
int number[6];
for (a=0;a<6;a++)
{
number[a]= a+5;
cout <<"number[ " <<a<< " ]"<<"is initialized with = " <<number[a]<<"n";
}
return 0;
}
10
Example of program
• It is possible to have arrays of more than one
dimension.
• Each dimension is represented as a subscript
in the array.
• Therefore, a two-dimensional array has two
subscripts.
11
Two Dimensional Array
 Type ArrayName [sizeofrow][sizeofcolumn];
Example : float price[4][5];
12
Array Declarations
When dimensioning a two dimensional table, always put the
number of rows first and number of column second.
float mark[5][2]={5,2.5,4,0,6,8,10,9.5,5.5,7};
int Bil[3][4]={{1,2,3,4},{11,12,13,14},{21,22,23,2}};
int age[ 2][ ]={{10,20,30},{10,30,55}};
13
Initializing Multidimensional
Arrays
float mark[5][2]={5,2.5,4,0,6,8,10,9.5,5.5,7};
mark[0][0]=5 mark[0][1]=2.5 mark[1][0]=4
mark[1][1]=0 mark[2][0]=6 mark[2][1]=8
mark[3][0]=10 mark[3][1]=9.5 mark[4][0]=5.5
mark[4][1]=7
[0] [1]
[0]
[1]
[2]
[3]
[4]
14
ILLUSTRATE TWO DIMENSIONAL
ARRAY
5 2.5
4 0
6 8
10 9.5
5.5 7
Accessing a two dimensional Array
15
# include <iostream>
using namespace std;
int main()
{
float mark[5][2]={5,2.5,4,0,6,8,10,9.5,5.5,7};
int i;
for( i=0;i<5;i++)
{
for(j=0;j<2;j++)
{
cout <<“mark :“<<mark[i][j]<<endl;
}
}
return 0;
}
16
Example of program
POINTER
POINTER
• Pointer is a variable which value is used to
point to another variable.
• It is able to assign different values to a pointer
variable and the value contained by a pointer
must be an address that indicates the location
of another variable in the memory.
• That is why a pointer is also called an address
variable.
WHAT & WHY of Pointers
• We use pointers as a technique:
- to develop lighter & faster programs.
- to optimally use the memory space.
• Pointers are also names (variable) – special ones.
• Can be likened to representatives of other variables - these include
ordinary variables, arrays, structures.
• Instead of directly pass or use the variables, pointers are used – why?
- Pointers are more lighter than the other variables.
- Pointers are more flexible – point to variable A, then point to B,
and point to C, and so on.
• no need to copy the values of the pointed variables, but only addresses
(location in RAM).
• If the variables contain data (values), pointers only contain the addresses
of the variables that they point to – hence the name is pointer.
Declaring pointer
• The general form of a pointer declaration:
data type *pointer_name;
• Data type specifies the type of data to which the
pointer points.
• Pointer name is the name of the pointer variable.
• Asterik (*) indicates the variable as a pointer.
Example:
char *ptrc;
int *no;
float *ptrnum2;
Assign the address of a variable to the
pointer
syntax:
pointerName=&VariableName;
Example:
ptrc=&c;
no=&num;
ptrnum2=&num1;
Working with Pointers
• How do we represent num1 (type of int) with
a pointer?
int num1; num1
600000
int * pnum1;
pnum1 500000
pnum1 = & num1;
1
2
3
600000pnum1
num1 600000
500000
Variable name is count and the value is 5.
Write pointer to assign the address of a variable to the
pointer.
int count=5;
int *ptrcount;
ptrcount=&count;
Example of program
#include<iostream>
using namespace std;
int main()
{
int number=5;
int *ptrn;
ptrn=&number;
cout<<“The value of variable number is:”<<number;
cout<<“nThe address of variable number is:”<<&number;
cout<<“The address of variable number is:”<<ptrpn;
cout<<“The value of variable number is:”<<*ptrn;
cout<<endl;
return 0;
}
The value of variable number is:5
The address of variable number is:0012FF7C
The address of variable number is:0012FF7C
The value of variable number is:5
Example of program
#include<iostream>
using namespace std;
int main()
{
int numb=50;
int *ptra;
ptra=&numb;
*ptra=*ptra+7;
cout<<“The manipulated value is:”<<*ptra;
return 0;
} The manipulated value is:57
Example 1: storing & displaying values
using pointers
int main()
{
int num1, num2;
int * pnum2;
pnum2 = & num2;
num1= 10;
*pnum2 = 20;
cout<<“nNum1: ”<< num1;
cout<<“nNum2: ”<< *pnum2;
return 0;
}
int main()
{
char sym1, sym2;
char * psym2;
psym2 = & sym2;
cin>> sym1;
cin>> * psym2;
cout<<“nSymbol 1: sym1;
cout<<“nSymbol 2: ”<< *psym2;
return 0;
}
New operator
• Provides a dynamic storage allocation in
computer memory location.
• Can be used to create variables that have no
identifiers to serve as their names.
• The no identifiers variables are referred to via
pointer.
Syntax:
pointerName=new dataType;
Example:
ptrP=new int;
Example of program
#include<iostream>
using namespace std;
int main()
{
int *pointer1, *pointer2;
pointer1=new int;
*pointer1=80;
pointer2=pointer1;
cout<<“The value of *pointer1 is:”<< *pointer1<<endl;
cout<<“The value of *pointer2 is:”<< *pointer2<<endl;
pointer1=new int;
*pointer1=100;
cout<<“The value of *pointer1 is:”<< *pointer1<<endl;
cout<<“The value of *pointer2 is:”<< *pointer2<<endl;
return 0;
}
The value of *pointer1 is:80
The value of *pointer2 is:80
The value of *pointer1 is:100
The value of *pointer2 is:80
Delete operator
• Eliminates a dynamic variable and return the
memory to the free store.
• This operation can be done if a program no
longer needs the dynamic variable.
• Now the free store memory can be reused to
create a new dynamic variable.
• Syntax:
delete pointer_name;
Example of program
#include<iostream>
using namespace std;
int main()
{
int *pointer1, *pointer2;
pointer1=new int;
*pointer1=80;
pointer2=pointer1;
cout<<“The value of *pointer1 is:”<< *pointer1<<endl;
cout<<“The value of *pointer2 is:”<< *pointer2<<endl;
delete pointer1;
cout<<“The value of *pointer1 is:”<< *pointer1<<endl;
cout<<“The value of *pointer2 is:”<< *pointer2<<endl;
cout<<“The address of *pointer1 is:”<< &pointer1<<endl;
cout<<“The address of *pointer2 is:”<< &pointer2<<endl;
return 0;
}
The value of *pointer1 is:80
The value of *pointer2 is:80
The value of *pointer1 is:-572662307
The value of *pointer2 is-572662307
The address of *pointer1 is:0012FF7C
The address of *pointer1 is:0012FF78
Relationship Between Arrays and
Pointers
Syntax:
pointerName=arrayName;
Example:
int age[5]={10,20,30,40,50};
int *ptrage;
ptrage=age;
The Relationship Between Arrays and
Pointers
We can visualize this array like this:
ptrage age
[0]
[1]
[2]
[3]
[4]
10
20
30
40
50
The Relationship Between Arrays and
Pointers
• Array name is starting address of array
int vals[] = {4, 7, 11};
starting address of vals: 0x4a00
cout << vals; // displays 0x4a00
cout << vals[0]; // displays 4
4 7 11
The Relationship Between Arrays and
Pointers
• Array name can be used as a pointer constant
int vals[] = {4, 7, 11};
cout << *vals; // displays 4
• Pointer can be used as an array name
int *ptrv = vals;
cout << ptrv[1]; // displays 7
Example of program
#include<iostream>
using namespace std;
int main()
{
int array[ ]={9,8,7,6,5,4};
int *pointer1;
pointer=array;
for( int a=0;a<6;a++)
{
cout<<“The address :”<< (pointer+a)<<“while the value is:”<<*(pointer+a)<<endl;
}
return 0;
}
The address: 0012FF68 while the value is: 9
The address: 0012FF6C while the value is: 8
The address: 0012FF70 while the value is: 7
The address: 0012FF74 while the value is: 6
The address: 0012FF78 while the value is: 5
The address: 0012FF7C while the value is: 4
Given:
int vals[]={4,7,11};
int *ptrv = vals;
• What is ptrv + n?
cout << *(ptrv+1); // displays 7
cout << *(ptrv+2); // displays 11
• Must use ( ) in expression
Array Access
• Array elements can be accessed in many ways.
Array access method Example
array name and [ ] vals[2] = 11;
pointer to array and [ ] ptrv[2] = 11;
array name and subscript
arithmetic
*(vals+2) = 11;
pointer to array and
subscript arithmetic
*(ptrv+2) = 11;
Array Access
• Array notation
vals[i]
• is equivalent to the pointer notation
*(ptrv + i) or ptrv[i]
Pointer Arithmetic
• Some arithmetic operators can be used
with pointers:
– Increment and decrement operators ++, --
– Integers can be added to or subtracted from
pointers using the operators +, -, +=, and -=
– One pointer can be subtracted from another by
using the subtraction operator -
Pointer Arithmetic
• Assume the variable definitions
int vals[]={4,7,11};
int *ptrv = vals;
• Examples of use of ++ and --
ptrv++; // points at 7
ptrv--; // now points at 4
Pointer Arithmetic
• Assume the variable definitions:
int vals[]={4,7,11};
int *ptrv = vals;
• Example of use of + to add an int to a
pointer:
cout << *(ptrv + 2)
– This statement will print 11
Pointer Arithmetic
• Assume the variable definitions:
int vals[]={4,7,11};
int *ptrv = vals;
• Example of use of +=:
ptrv = vals; // points at 4
ptrv += 2; // points at 11
Example of program:
STRUCTURE
• Structure is a collection of variable under a
single name.
• Variable can be of any type: int, float, char etc.
• The main difference between structure and
array is :
– Array are collection of the same data type.
– Structure is a collection of variable under a single
name.
Declaring a structure
• The structure is declared by using the keyword
struct followed by structure name, also called
a tag.
• Then the structure members (variable) are
defined with their type and variable names
inside the open and close braces { and }.
• Finally, the closed braces end with a
semicolon denoted as ; following the
statement.
example
• Structure name is customer
• Three variables:
- custnum of type int
- salary of type float structure members
- commission of type double
This structure is declared as follows:
struct customer
{
int custnum;
float salary ;
double commission;
};
Declare structure variable
• This similar to variable declaration
• For variable declaration, data type is defined
followed by variable name.
• For structure variable declaration, the data
type is the name of the structure followed by
the structure variable name.
structure_name structure_variable _name
struct customer
{
int custnum;
float salary ;
double commission;
}; customer cust1; structure variable
Assign value to a structure variable
THREE ways to assign values to the elements:
i) in the declaration of a record variable
customer cust1 = {111, 1500, 25.50};
ii) direct assignment
cust1.custnum = 111;
cust1.salary = 1500;
cust1.commission= 25.50;
iii) input statement
cin>>cust1.custnum;
cin>>cust1.salary;
cin>>cust1.commission;
Access values in structure variable
• Elements are accessed by their name.
• Syntax:
structure_variable.variable_name;
• Example:
cust1.custnum;
• The dot is called the member selector
Array vs. Structure

More Related Content

What's hot

Pointers
PointersPointers
Pointers
sanya6900
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
 
Pointer
PointerPointer
Pointer
Fahuda E
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer웅식 전
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
Mohammed Khan
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
Faisal Shahzad Khan
 
Very interesting C programming Technical Questions
Very interesting C programming Technical Questions Very interesting C programming Technical Questions
Very interesting C programming Technical Questions
Vanathi24
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointersvinay arora
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Ameer Hamxa
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
Ilio Catallo
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structureSaad Gabr
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
minal kumar soni
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
chanchal ghosh
 
Arrays in c
Arrays in cArrays in c
Arrays in c
CHANDAN KUMAR
 

What's hot (20)

Pointers
PointersPointers
Pointers
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointer
PointerPointer
Pointer
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
Very interesting C programming Technical Questions
Very interesting C programming Technical Questions Very interesting C programming Technical Questions
Very interesting C programming Technical Questions
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Lec2
Lec2Lec2
Lec2
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 

Similar to POLITEKNIK MALAYSIA

Pointer
PointerPointer
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
20.C++Pointer.pptx
20.C++Pointer.pptx20.C++Pointer.pptx
20.C++Pointer.pptx
AtharvPotdar2
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Kashif Nawab
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
NUST Stuff
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
TamiratDejene1
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
JumanneChiyanda
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
sajinis3
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
Osmania University
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
Vivek Bhargav
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
janithlakshan1
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
ShivamChaturvedi67
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
Ramakrishna Reddy Bijjam
 
Savitch Ch 07
Savitch Ch 07Savitch Ch 07
Savitch Ch 07
Terry Yoast
 

Similar to POLITEKNIK MALAYSIA (20)

Pointers
PointersPointers
Pointers
 
Pointer
PointerPointer
Pointer
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
 
20.C++Pointer.pptx
20.C++Pointer.pptx20.C++Pointer.pptx
20.C++Pointer.pptx
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec2
Lec2Lec2
Lec2
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Savitch Ch 07
Savitch Ch 07Savitch Ch 07
Savitch Ch 07
 
Savitch Ch 07
Savitch Ch 07Savitch Ch 07
Savitch Ch 07
 

More from Aiman Hud

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 

More from Aiman Hud (20)

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 

Recently uploaded

How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 

Recently uploaded (20)

How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 

POLITEKNIK MALAYSIA

  • 2. • An array is a collection of data storage locations, which holds the same type of data. Each storage location is called an element of the array 2 Introduction To Array
  • 3. • An array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value. • General form to declare an array Data_type Array_name [ Array_size]; – Data_type is the specifier that indicates what data type . – Array_name is the name of the declared array. – Array_size defines how many elements the array can contain 3 Array Declarations
  • 4. float price[10]; float specifies as the data type price is the name of the declared array the size of the array is 10 char name [50]; int age [20]; 4 Example:
  • 5. • int x[6]={ 1, 13, 33, 7, 20, 6}; – The integer inside the braces { and } are assigned to the corresponding elements of the array. 5 Initializing An Array
  • 6. 1 13 33 7 20 6 6 Illustrate one dimensional array int x[6]={ 1, 13, 33, 7, 20, 6}; x[0] x[1] x[2] x[3] x[4] x[5] index Element 2 Element 3 Element 4 Element 5 Element 6Element 1
  • 7. int x [9] = {0,3,7,3,0,20,61,17,800 }; double x [8] = {1.2, 1.4, 1.7, 2.6, 3.4, 2.6, 1.2, 3.1}; char x[9] = {‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’}; 7 Example:
  • 8. # include <iostream> using namespace std; int main() { float price[3]; cout <<“price 1:"; cin>>price[0]; cout <<“price 2:"; cin>>price[1]; cout <<“price 3:"; cin>>price[2]; cout<<“TOTAL PRICE:”<<price[0]+price[1]+price[2]; } return 0; } 8 Example of program
  • 9. # include <iostream> using namespace std; int main() { float price[3]; int i; total=0; for( i=0;i<3;i++) { cout <<“price :“<<0+i<<; cin>>price[i]; total=total+price[i]; } cout<<“TOTAL PRICE:”<<total; return 0; } 9 Example of program
  • 10. # include <iostream> using namespace std; main() { int a; int number[6]; for (a=0;a<6;a++) { number[a]= a+5; cout <<"number[ " <<a<< " ]"<<"is initialized with = " <<number[a]<<"n"; } return 0; } 10 Example of program
  • 11. • It is possible to have arrays of more than one dimension. • Each dimension is represented as a subscript in the array. • Therefore, a two-dimensional array has two subscripts. 11 Two Dimensional Array
  • 12.  Type ArrayName [sizeofrow][sizeofcolumn]; Example : float price[4][5]; 12 Array Declarations When dimensioning a two dimensional table, always put the number of rows first and number of column second.
  • 13. float mark[5][2]={5,2.5,4,0,6,8,10,9.5,5.5,7}; int Bil[3][4]={{1,2,3,4},{11,12,13,14},{21,22,23,2}}; int age[ 2][ ]={{10,20,30},{10,30,55}}; 13 Initializing Multidimensional Arrays
  • 14. float mark[5][2]={5,2.5,4,0,6,8,10,9.5,5.5,7}; mark[0][0]=5 mark[0][1]=2.5 mark[1][0]=4 mark[1][1]=0 mark[2][0]=6 mark[2][1]=8 mark[3][0]=10 mark[3][1]=9.5 mark[4][0]=5.5 mark[4][1]=7 [0] [1] [0] [1] [2] [3] [4] 14 ILLUSTRATE TWO DIMENSIONAL ARRAY 5 2.5 4 0 6 8 10 9.5 5.5 7
  • 15. Accessing a two dimensional Array 15
  • 16. # include <iostream> using namespace std; int main() { float mark[5][2]={5,2.5,4,0,6,8,10,9.5,5.5,7}; int i; for( i=0;i<5;i++) { for(j=0;j<2;j++) { cout <<“mark :“<<mark[i][j]<<endl; } } return 0; } 16 Example of program
  • 18. POINTER • Pointer is a variable which value is used to point to another variable. • It is able to assign different values to a pointer variable and the value contained by a pointer must be an address that indicates the location of another variable in the memory. • That is why a pointer is also called an address variable.
  • 19. WHAT & WHY of Pointers • We use pointers as a technique: - to develop lighter & faster programs. - to optimally use the memory space. • Pointers are also names (variable) – special ones. • Can be likened to representatives of other variables - these include ordinary variables, arrays, structures. • Instead of directly pass or use the variables, pointers are used – why? - Pointers are more lighter than the other variables. - Pointers are more flexible – point to variable A, then point to B, and point to C, and so on. • no need to copy the values of the pointed variables, but only addresses (location in RAM). • If the variables contain data (values), pointers only contain the addresses of the variables that they point to – hence the name is pointer.
  • 20. Declaring pointer • The general form of a pointer declaration: data type *pointer_name; • Data type specifies the type of data to which the pointer points. • Pointer name is the name of the pointer variable. • Asterik (*) indicates the variable as a pointer.
  • 22. Assign the address of a variable to the pointer syntax: pointerName=&VariableName; Example: ptrc=&c; no=&num; ptrnum2=&num1;
  • 23. Working with Pointers • How do we represent num1 (type of int) with a pointer? int num1; num1 600000 int * pnum1; pnum1 500000 pnum1 = & num1; 1 2 3 600000pnum1 num1 600000 500000
  • 24. Variable name is count and the value is 5. Write pointer to assign the address of a variable to the pointer. int count=5; int *ptrcount; ptrcount=&count;
  • 25. Example of program #include<iostream> using namespace std; int main() { int number=5; int *ptrn; ptrn=&number; cout<<“The value of variable number is:”<<number; cout<<“nThe address of variable number is:”<<&number; cout<<“The address of variable number is:”<<ptrpn; cout<<“The value of variable number is:”<<*ptrn; cout<<endl; return 0; } The value of variable number is:5 The address of variable number is:0012FF7C The address of variable number is:0012FF7C The value of variable number is:5
  • 26. Example of program #include<iostream> using namespace std; int main() { int numb=50; int *ptra; ptra=&numb; *ptra=*ptra+7; cout<<“The manipulated value is:”<<*ptra; return 0; } The manipulated value is:57
  • 27. Example 1: storing & displaying values using pointers int main() { int num1, num2; int * pnum2; pnum2 = & num2; num1= 10; *pnum2 = 20; cout<<“nNum1: ”<< num1; cout<<“nNum2: ”<< *pnum2; return 0; } int main() { char sym1, sym2; char * psym2; psym2 = & sym2; cin>> sym1; cin>> * psym2; cout<<“nSymbol 1: sym1; cout<<“nSymbol 2: ”<< *psym2; return 0; }
  • 28. New operator • Provides a dynamic storage allocation in computer memory location. • Can be used to create variables that have no identifiers to serve as their names. • The no identifiers variables are referred to via pointer.
  • 30. Example of program #include<iostream> using namespace std; int main() { int *pointer1, *pointer2; pointer1=new int; *pointer1=80; pointer2=pointer1; cout<<“The value of *pointer1 is:”<< *pointer1<<endl; cout<<“The value of *pointer2 is:”<< *pointer2<<endl; pointer1=new int; *pointer1=100; cout<<“The value of *pointer1 is:”<< *pointer1<<endl; cout<<“The value of *pointer2 is:”<< *pointer2<<endl; return 0; } The value of *pointer1 is:80 The value of *pointer2 is:80 The value of *pointer1 is:100 The value of *pointer2 is:80
  • 31. Delete operator • Eliminates a dynamic variable and return the memory to the free store. • This operation can be done if a program no longer needs the dynamic variable. • Now the free store memory can be reused to create a new dynamic variable.
  • 33. Example of program #include<iostream> using namespace std; int main() { int *pointer1, *pointer2; pointer1=new int; *pointer1=80; pointer2=pointer1; cout<<“The value of *pointer1 is:”<< *pointer1<<endl; cout<<“The value of *pointer2 is:”<< *pointer2<<endl; delete pointer1; cout<<“The value of *pointer1 is:”<< *pointer1<<endl; cout<<“The value of *pointer2 is:”<< *pointer2<<endl; cout<<“The address of *pointer1 is:”<< &pointer1<<endl; cout<<“The address of *pointer2 is:”<< &pointer2<<endl; return 0; } The value of *pointer1 is:80 The value of *pointer2 is:80 The value of *pointer1 is:-572662307 The value of *pointer2 is-572662307 The address of *pointer1 is:0012FF7C The address of *pointer1 is:0012FF78
  • 34. Relationship Between Arrays and Pointers Syntax: pointerName=arrayName; Example: int age[5]={10,20,30,40,50}; int *ptrage; ptrage=age;
  • 35. The Relationship Between Arrays and Pointers We can visualize this array like this: ptrage age [0] [1] [2] [3] [4] 10 20 30 40 50
  • 36. The Relationship Between Arrays and Pointers • Array name is starting address of array int vals[] = {4, 7, 11}; starting address of vals: 0x4a00 cout << vals; // displays 0x4a00 cout << vals[0]; // displays 4 4 7 11
  • 37. The Relationship Between Arrays and Pointers • Array name can be used as a pointer constant int vals[] = {4, 7, 11}; cout << *vals; // displays 4 • Pointer can be used as an array name int *ptrv = vals; cout << ptrv[1]; // displays 7
  • 38. Example of program #include<iostream> using namespace std; int main() { int array[ ]={9,8,7,6,5,4}; int *pointer1; pointer=array; for( int a=0;a<6;a++) { cout<<“The address :”<< (pointer+a)<<“while the value is:”<<*(pointer+a)<<endl; } return 0; } The address: 0012FF68 while the value is: 9 The address: 0012FF6C while the value is: 8 The address: 0012FF70 while the value is: 7 The address: 0012FF74 while the value is: 6 The address: 0012FF78 while the value is: 5 The address: 0012FF7C while the value is: 4
  • 39. Given: int vals[]={4,7,11}; int *ptrv = vals; • What is ptrv + n? cout << *(ptrv+1); // displays 7 cout << *(ptrv+2); // displays 11 • Must use ( ) in expression
  • 40. Array Access • Array elements can be accessed in many ways. Array access method Example array name and [ ] vals[2] = 11; pointer to array and [ ] ptrv[2] = 11; array name and subscript arithmetic *(vals+2) = 11; pointer to array and subscript arithmetic *(ptrv+2) = 11;
  • 41. Array Access • Array notation vals[i] • is equivalent to the pointer notation *(ptrv + i) or ptrv[i]
  • 42. Pointer Arithmetic • Some arithmetic operators can be used with pointers: – Increment and decrement operators ++, -- – Integers can be added to or subtracted from pointers using the operators +, -, +=, and -= – One pointer can be subtracted from another by using the subtraction operator -
  • 43. Pointer Arithmetic • Assume the variable definitions int vals[]={4,7,11}; int *ptrv = vals; • Examples of use of ++ and -- ptrv++; // points at 7 ptrv--; // now points at 4
  • 44. Pointer Arithmetic • Assume the variable definitions: int vals[]={4,7,11}; int *ptrv = vals; • Example of use of + to add an int to a pointer: cout << *(ptrv + 2) – This statement will print 11
  • 45. Pointer Arithmetic • Assume the variable definitions: int vals[]={4,7,11}; int *ptrv = vals; • Example of use of +=: ptrv = vals; // points at 4 ptrv += 2; // points at 11
  • 47. STRUCTURE • Structure is a collection of variable under a single name. • Variable can be of any type: int, float, char etc. • The main difference between structure and array is : – Array are collection of the same data type. – Structure is a collection of variable under a single name.
  • 48. Declaring a structure • The structure is declared by using the keyword struct followed by structure name, also called a tag. • Then the structure members (variable) are defined with their type and variable names inside the open and close braces { and }. • Finally, the closed braces end with a semicolon denoted as ; following the statement.
  • 49. example • Structure name is customer • Three variables: - custnum of type int - salary of type float structure members - commission of type double
  • 50. This structure is declared as follows: struct customer { int custnum; float salary ; double commission; };
  • 51. Declare structure variable • This similar to variable declaration • For variable declaration, data type is defined followed by variable name. • For structure variable declaration, the data type is the name of the structure followed by the structure variable name. structure_name structure_variable _name
  • 52. struct customer { int custnum; float salary ; double commission; }; customer cust1; structure variable
  • 53. Assign value to a structure variable THREE ways to assign values to the elements: i) in the declaration of a record variable customer cust1 = {111, 1500, 25.50}; ii) direct assignment cust1.custnum = 111; cust1.salary = 1500; cust1.commission= 25.50; iii) input statement cin>>cust1.custnum; cin>>cust1.salary; cin>>cust1.commission;
  • 54. Access values in structure variable • Elements are accessed by their name. • Syntax: structure_variable.variable_name; • Example: cust1.custnum; • The dot is called the member selector