SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
1. C++ Scope Resolution Operator:: 
2. Pointers 
C++ Scope Resolution Operator:: 
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=% 
2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm 
The :: (scope resolution) operator is used to qualify 
hidden names so that you can still use them. You can 
use the unary scope operator if a namespace scope or 
global scope name is hidden by an explicit declaration 
of the same name in a block or class. For example: 
int count = 0; 
int main(void) { 
int count = 0; 
::count = 1; // set global count to 1 
count = 2; // set local count to 2 
return 0; 
} 
 The declaration of count declared in the main() 
function hides the integer named count declared 
in global namespace scope. 
 The statement ::count = 1 accesses the 
variable named count declared in global 
namespace scope. 
 You can also use the class scope operator to 
qualify class names or class member names.
 If a class member name is hidden, you can use it 
by qualifying it with its class name and the class 
scope operator. 
Example: 
The declaration of the variable X hides the class type 
X, but you can still use the static class member count 
by qualifying it with the class type X and the scope 
resolution operator. 
#include <iostream> 
using namespace std; 
class X 
{ 
public: 
static int count; 
}; 
int X::count = 10;// define static data member 
int main () 
{ 
int X = 0; // hides class type X 
// use static member of class X 
cout << X::count << endl; 
}
C++ Scope Resolution Operator :: 
PURPOSE 
C++The :: (scope resolution) operator is used to 
qualify hidden names so that you can still use 
them. 
You can use the unary scope operator if a 
namespace scope or global scope name is 
hidden by an explicit declaration of the same 
name in a block or class. 
Namespaces 
Namespaces allow to group entities like classes, 
objects and functions under a name. 
In this way the global scope can be divided in 
"sub-scopes", each one with its own name. 
The format of namespaces is: 
namespace identifier 
{ 
entities 
} 
Where identifier is any valid identifier and 
entities is the set of classes, objects and 
functions that are included within the 
namespace.
For example: 
namespace myNamespace 
{ 
int a, b; 
} 
In this case, the variables a and b are normal 
variables declared within a namespace called 
myNamespace. 
In order to access these variables from outside 
the myNamespace namespace we have to use 
the scope operator :: 
For example, to access the previous variables 
from outside [myNamespace=] we can write: 
general::a 
general::b 
The functionality of namespaces is especially 
useful in the case that there is a possibility that 
a global object or function uses the same 
identifier as another one, causing redefinition 
errors.
For example: 
// namespaces 
#include <iostream> 
using namespace std; 
namespace first 
{ 
int var = 5; 
} 
namespace second 
{ 
double var = 3.1416; 
} 
int main () { 
cout << first::var << endl; 
cout << second::var << endl; 
return 0; 
} 
EXAMPLE OUTPUT: 
5 
3.1416 
In this case, there are two global variables with 
the same name: var. One 
is defined as an int within the namespace first 
and the other one in defined 
as a double in the namespace called second. No 
redefinition errors happen 
thanks to namespaces.
The 'using' Keyword 
The keyword using is used to introduce a name 
from a namespace into 
the current declarative region. For example: 
// using example 
#include <iostream> 
using namespace std; 
namespace first 
{ 
int x = 5; 
int y = 10; 
} 
namespace second 
{ 
double x = 3.1416; 
double y = 2.7183; 
} 
int main () { 
using first::x; 
using second::y 
cout << x << endl; 
cout << y << endl; 
cout << first::y << endl; 
cout << second::x << endl; 
return 0; 
} 
EXAMPLE OUTPUT: 
5 
2.7183 
10 
2.7183
Notice how in this code, x (without any name 
qualifier) refers to first::x 
whereas y refers to second::y, exactly as our 
using declarations have 
specified. We still have access to first::y and 
second::x using their 
fully qualified names. 
The keyword using can also be used as a 
directive to introduce an entire 
namespace: 
// using example 
#include <iostream> 
using namespace std; 
namespace first 
{ 
int x = 5; 
int y = 10; 
} 
namespace second 
{ 
double x = 3.1416; 
double y = 2.7183; 
} 
int main () { 
using namespace first; 
cout << x << endl; 
cout << y << endl; 
cout << second::y << endl; 
cout << second::x << endl; 
return 0; 
}
EXAMPLE OUTPUT: 
5 
10 
3.1416 
2.7183 
In this case, since we have declared that we 
were using namespace first, all direct uses of x 
and y without name qualifiers were referring to 
their declarations in namespace first. 
using and using namespace have validity 
only in the same block in which they are stated 
or in the entire code if they are used directly in 
the global scope. 
For example, if we had the intention to first use 
the objects of one namespace and then those of 
another one, we could do something like:
// using namespace example 
#include <iostream> 
using namespace std; 
namespace first 
{ 
int x = 5; 
} 
namespace second 
{ 
double x = 3.1416; 
} 
int main () 
{ 
// Here are two blocks 
{ 
using namespace first; 
cout << x << endl; 
} 
{ 
using namespace second; 
cout << x << endl; 
} 
return 0; 
} 
EXAMPLE OUTPUT: 
5 
3.1416
What Are Pointers? 
Reference: 
http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm 
 C++ pointers are easy and fun to learn. 
 Some C++ tasks are performed more easily with 
pointers, and 
 other C++ tasks, such as dynamic memory allocation, 
cannot be performed without them. 
As you know every variable is a memory location and every 
memory location has its address defined which can be 
accessed using 
ampersand (&) operator which denotes an address in 
memory. Consider the following which will print the address 
of the variables defined:#include <iostream> 
using namespace std; 
int main () 
{ 
int var1; 
char var2[10]; 
cout << "Address of var1 variable: "; 
cout << &var1 << endl; 
cout << "Address of var2 variable: "; 
cout << &var2 << endl; 
return 0; 
} 
When the above code is compiled and executed, it produces result something as follows: 
Address of var1 variable: 0xbfebd5c0 
Address of var2 variable: 0xbfebd5b6
What Are Pointers? 
 A pointer is a variable whose value is the address of 
another variable. 
 Like any variable or constant, you must declare a 
pointer before you can work with it. 
 The general form of a pointer variable declaration is: 
type *variablename; 
Here, type is the pointer's base type; it must be a valid C++ type and 
variablename is the name of the pointer variable. 
The asterisk you used to declare a pointer is the same asterisk that you use for 
multiplication. 
However, in this statement the asterisk is being used to designate a variable as 
a pointer. Following are the valid pointer declaration: 
int *ip; // pointer to an integer 
double *dp; // pointer to a double 
float *fp; // pointer to a float 
char *ch // pointer to character 
The actual data type of the value of all pointers, whether 
integer, float, character, or otherwise, is the same, a long 
hexadecimal number that represents a memory address. The 
only difference between pointers of different data types is 
the data type of the variable or constant that the pointer 
points to.
There are few important operations Using Pointers in C++: 
Which we will do with the pointers very frequently: 
(a) We define pointer variables 
(b) Assign the address of a variable to a pointer and 
(c) Finally access the value at the address available in the pointer variable. 
This is done by using unary operator * that returns the value of the variable 
located at the address specified by its operand. Following example makes use 
of these operations: 
#include <iostream> 
using namespace std; 
int main () 
{ 
int var = 20; // actual variable declaration. 
int *ip; // pointer variable 
ip = &var; // store address of var in pointer variable 
cout << "Value of var variable: "; 
cout << var << endl; 
// print the address stored in ip pointer variable 
cout << "Address stored in ip variable: "; 
cout << ip << endl; 
// access the value at the address available in pointer 
cout << "Value of *ip variable: "; 
cout << *ip << endl; 
return 0; 
}
When the above code is compiled and executed, it produces 
result something as follows: 
Value of var variable: 20 
Address stored in ip variable: 0xbfc601ac 
Value of *ip variable: 20 
C++ Pointers in Detail: 
Pointers have many but easy concepts and they are very 
important to C++ programming. There are following 
few important pointer concepts which should be clear 
to a C++ programmer: 
Concept Description 
C++ Null Pointers 
C++ supports null pointer, which is a 
constant with a value of zero defined in 
several standard libraries. 
C++ pointer arithmetic 
There are four arithmetic operators 
that can be used on pointers: ++, --, +, - 
C++ pointers vs arrays 
There is a close relationship between 
pointers and arrays. Let us check how? 
C++ array of pointers 
You can define arrays to hold a number 
of pointers. 
C++ pointer to pointer 
C++ allows you to have pointer on a 
pointer and so on. 
Passing pointers to 
functions 
Passing an argument by reference or by 
address both enable the passed 
argument to be changed in the calling 
function by the called function. 
Return pointer from 
functions 
C++ allows a function to return a 
pointer to local variable, static variable 
and dynamically allocated memory as 
well.
C++ NULL Pointers 
 It is always a good practice to assign the pointer NULL to 
a pointer variable in case you do not have exact address 
to be assigned. 
 This is done at the time of variable declaration. A pointer 
that is assigned NULL is called a null pointer. 
 The NULL pointer is a constant with a value of zero 
defined in several standard libraries, including iostream. 
Consider the following program: 
#include <iostream> 
using namespace std; 
int main () 
{ 
int *ptr = NULL; 
cout << "The value of ptr is " << ptr ; 
return 0; 
} 
When the above code is compiled and executed, it 
produces following result: 
The value of ptr is 0 
To check for a null pointer you can use an if statement as follows: 
if(ptr) // succeeds if p is not null 
if(!ptr) // succeeds if p is null
C++ Pointer Arithmetics 
 As you understood pointer is an address which is a 
numeric value. Therefore, you can perform arithmetic 
operations on a pointer just as you can a numeric value. 
 There are four arithmetic operators that can be used on 
pointers: ++, --, +, and - 
ptr++ 
 the ptr will point to the location 1004 because each time 
ptr is incremented, it will point to the next integer 
. 
 This operation will move the pointer to next memory 
location without impacting actual value at the memory 
location. 
 If ptr points to a character whose address is 1000, then 
above operation will point to the location 1001 because 
next character will be available at 1001.
Incrementing a Pointer: 
 We prefer using a pointer in our program instead of an array 
because the variable pointer can be incremented, unlike the 
array name which cannot be incremented because it is a 
constant pointer. 
 The following program increments the variable pointer to 
access each succeeding element of the array: 
#include <iostream> 
using namespace std; 
const int MAX = 3; 
int main () 
{ 
int var[MAX] = {10, 100, 200}; 
int *ptr; 
// let us have array address in pointer. 
ptr = var; 
for (int i = 0; i < MAX; i++) 
{ 
cout << "Address of var[" << i << "] = "; 
cout << ptr << endl; 
cout << "Value of var[" << i << "] = "; 
cout << *ptr << endl; 
// point to the next location 
ptr++; 
} 
return 0; 
}
Pointers and arrays are strongly related. 
In fact, pointers and arrays are interchangeable in many cases. 
For example, a pointer that points to the beginning of an array 
can access that array by using either pointer arithmetic or 
array-style indexing. Consider the following program: 
#include <iostream> 
using namespace std; 
const int MAX = 3; 
int main () 
{ 
int var[MAX] = {10, 100, 200}; 
int *ptr; 
// let us have array address in pointer. 
ptr = var; 
for (int i = 0; i < MAX; i++) 
{ 
cout << "Address of var[" << i << "] = "; 
cout << ptr << endl; 
cout << "Value of var[" << i << "] = "; 
cout << *ptr << endl; 
// point to the next location 
ptr++; 
} 
return 0; 
}
When the above code is compiled and executed, it 
produces result something as follows: 
Address of var[0] = 0xbfa088b0 
Value of var[0] = 10 
Address of var[1] = 0xbfa088b4 
Value of var[1] = 100 
Address of var[2] = 0xbfa088b8 
Value of var[2] = 200 
MORE EXAMPLES 
#include <iostream> 
using namespace std; 
const int MAX = 3; 
int main () 
{ 
int var[MAX] = {10, 100, 200}; 
int *ptr[MAX]; 
for (int i = 0; i < MAX; i++) 
{ 
ptr[i] = &var[i]; // assign the address of integer. 
} 
for (int i = 0; i < MAX; i++) 
{ 
cout << "Value of var[" << i << "] = "; 
cout << *ptr[i] << endl; 
} 
return 0; 
} 
When the above code is compiled and executed, it produces following 
result: 
Value of var[0] = 10 
Value of var[1] = 100 
Value of var[2] = 200 
You can also use an array of pointers to character to store 
a list of strings as follows: 
#include <iostream>
using namespace std; 
const int MAX = 4; 
int main () 
{ 
char *names[MAX] = { 
"Zara Ali", 
"Hina Ali", 
"Nuha Ali", 
"Sara Ali", 
}; 
for (int i = 0; i < MAX; i++) 
{ 
cout << "Value of names[" << i << "] = "; 
cout << names[i] << endl; 
} 
return 0; 
} 
When the above code is compiled and executed, it produces following result: 
Value of names[0] = Zara Ali 
Value of names[1] = Hina Ali 
Value of names[2] = Nuha Ali 
Value of names[3] = Sara Ali

More Related Content

PPTX
Shortest path algorithm
PPTX
Critical section problem in operating system.
PPT
Sum of subsets problem by backtracking 
PPTX
Classes objects in java
PDF
OLAP in Data Warehouse
PPTX
Nested loops
PPTX
Mathematical Analysis of Recursive Algorithm.
PPTX
C data types, arrays and structs
Shortest path algorithm
Critical section problem in operating system.
Sum of subsets problem by backtracking 
Classes objects in java
OLAP in Data Warehouse
Nested loops
Mathematical Analysis of Recursive Algorithm.
C data types, arrays and structs

What's hot (20)

PPTX
Java package
PPT
Unit 3 Network Layer PPT
PPTX
Mathematical Analysis of Non-Recursive Algorithm.
PPTX
Decision tree induction \ Decision Tree Algorithm with Example| Data science
PPTX
Paging and segmentation
PPT
Complexity of Algorithm
PPTX
Point to-point protocol (ppp)
DOC
operating system lecture notes
PDF
DC_M5_L2_Data Centric Consistency (1).pdf
PPTX
single linked list
PPTX
Graph coloring using backtracking
PPTX
Design and Analysis of Algorithms.pptx
PPT
Data Structures- Part5 recursion
PPTX
Segmentation in Operating Systems.
PDF
ADA complete notes
PPTX
Ipv4 presentation
PPTX
Distribution transparency and Distributed transaction
PPTX
Data types in c++
PDF
Major and Minor Elements of Object Model
PDF
Authentication techniques
Java package
Unit 3 Network Layer PPT
Mathematical Analysis of Non-Recursive Algorithm.
Decision tree induction \ Decision Tree Algorithm with Example| Data science
Paging and segmentation
Complexity of Algorithm
Point to-point protocol (ppp)
operating system lecture notes
DC_M5_L2_Data Centric Consistency (1).pdf
single linked list
Graph coloring using backtracking
Design and Analysis of Algorithms.pptx
Data Structures- Part5 recursion
Segmentation in Operating Systems.
ADA complete notes
Ipv4 presentation
Distribution transparency and Distributed transaction
Data types in c++
Major and Minor Elements of Object Model
Authentication techniques
Ad

Viewers also liked (18)

PPT
Operators in C++
PPTX
C++ io manipulation
PDF
Cement Industries
PDF
Material and energy balance
PPTX
C++ operator
PPTX
Data members and member functions
PDF
Carbon dioxide Industries
PDF
Sodium carbonate Industries
PDF
Chemical engineering Introduction to Process Calculations Stoichiometry
PDF
Material and Energy Balance
PDF
Rehabilitación de Pozos Petroleros
PDF
Sulfuric acid Industries
PDF
Introduction to Chemical Engineering
PDF
Process design for chemical engineers
PPTX
Concept Of C++ Data Types
 
PPTX
Abstract Base Class and Polymorphism in C++
PPTX
Polymorphism
PDF
Polymorphism
Operators in C++
C++ io manipulation
Cement Industries
Material and energy balance
C++ operator
Data members and member functions
Carbon dioxide Industries
Sodium carbonate Industries
Chemical engineering Introduction to Process Calculations Stoichiometry
Material and Energy Balance
Rehabilitación de Pozos Petroleros
Sulfuric acid Industries
Introduction to Chemical Engineering
Process design for chemical engineers
Concept Of C++ Data Types
 
Abstract Base Class and Polymorphism in C++
Polymorphism
Polymorphism
Ad

Similar to 18 dec pointers and scope resolution operator (20)

PPT
C++ Programming Course
PPT
Ccourse 140618093931-phpapp02
PDF
Introduction to C++
PPTX
Object oriented programming slides for presentation
PDF
C++ Pointers , Basic to advanced Concept
DOCX
C++ Pointers with Examples.docx
PPSX
C++ quik notes
PPTX
Pointers, virtual function and polymorphism
PPTX
Pointers in C++ object oriented programming
PPTX
Introduction to pointers in c plus plus .
PPT
C96e1 session3 c++
PPT
Pointer
PPTX
C++ - UNIT_-_IV.pptx which contains details about Pointers
PDF
Object Oriented Programming using C++ - Part 4
PPTX
pointer, virtual function and polymorphism
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
PDF
Object Oriented Programming (OOP) using C++ - Lecture 4
PDF
Chapter 2 - Introduction to Pointer Variables - Student.pdf
PPTX
Operators
PPTX
c++ introduction, array, pointers included.pptx
C++ Programming Course
Ccourse 140618093931-phpapp02
Introduction to C++
Object oriented programming slides for presentation
C++ Pointers , Basic to advanced Concept
C++ Pointers with Examples.docx
C++ quik notes
Pointers, virtual function and polymorphism
Pointers in C++ object oriented programming
Introduction to pointers in c plus plus .
C96e1 session3 c++
Pointer
C++ - UNIT_-_IV.pptx which contains details about Pointers
Object Oriented Programming using C++ - Part 4
pointer, virtual function and polymorphism
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Object Oriented Programming (OOP) using C++ - Lecture 4
Chapter 2 - Introduction to Pointer Variables - Student.pdf
Operators
c++ introduction, array, pointers included.pptx

More from SAFFI Ud Din Ahmad (13)

PDF
Ammonia Industries
PDF
Phosphoric acid Industries
PDF
Oxygen and nitrogen Industries
PDF
Nitric acid Industries
PDF
Hydrogen Industries
PDF
Fertilizers Industries
PDF
Cuastic soda and Chlorine Industries
PPTX
Motion of particles in fluid (GIKI)
PPTX
Size reduction (GIKI)
PPTX
Principles of Combustion (GIKI)
PPTX
Liquid Fuels Lectures (GIKI)
PPTX
Particle Technology Lectures GIKI
PPTX
Fuels and Combustion Lectures (GIKI)
Ammonia Industries
Phosphoric acid Industries
Oxygen and nitrogen Industries
Nitric acid Industries
Hydrogen Industries
Fertilizers Industries
Cuastic soda and Chlorine Industries
Motion of particles in fluid (GIKI)
Size reduction (GIKI)
Principles of Combustion (GIKI)
Liquid Fuels Lectures (GIKI)
Particle Technology Lectures GIKI
Fuels and Combustion Lectures (GIKI)

18 dec pointers and scope resolution operator

  • 1. 1. C++ Scope Resolution Operator:: 2. Pointers C++ Scope Resolution Operator:: http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=% 2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. For example: int count = 0; int main(void) { int count = 0; ::count = 1; // set global count to 1 count = 2; // set local count to 2 return 0; }  The declaration of count declared in the main() function hides the integer named count declared in global namespace scope.  The statement ::count = 1 accesses the variable named count declared in global namespace scope.  You can also use the class scope operator to qualify class names or class member names.
  • 2.  If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator. Example: The declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator. #include <iostream> using namespace std; class X { public: static int count; }; int X::count = 10;// define static data member int main () { int X = 0; // hides class type X // use static member of class X cout << X::count << endl; }
  • 3. C++ Scope Resolution Operator :: PURPOSE C++The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. Namespaces Namespaces allow to group entities like classes, objects and functions under a name. In this way the global scope can be divided in "sub-scopes", each one with its own name. The format of namespaces is: namespace identifier { entities } Where identifier is any valid identifier and entities is the set of classes, objects and functions that are included within the namespace.
  • 4. For example: namespace myNamespace { int a, b; } In this case, the variables a and b are normal variables declared within a namespace called myNamespace. In order to access these variables from outside the myNamespace namespace we have to use the scope operator :: For example, to access the previous variables from outside [myNamespace=] we can write: general::a general::b The functionality of namespaces is especially useful in the case that there is a possibility that a global object or function uses the same identifier as another one, causing redefinition errors.
  • 5. For example: // namespaces #include <iostream> using namespace std; namespace first { int var = 5; } namespace second { double var = 3.1416; } int main () { cout << first::var << endl; cout << second::var << endl; return 0; } EXAMPLE OUTPUT: 5 3.1416 In this case, there are two global variables with the same name: var. One is defined as an int within the namespace first and the other one in defined as a double in the namespace called second. No redefinition errors happen thanks to namespaces.
  • 6. The 'using' Keyword The keyword using is used to introduce a name from a namespace into the current declarative region. For example: // using example #include <iostream> using namespace std; namespace first { int x = 5; int y = 10; } namespace second { double x = 3.1416; double y = 2.7183; } int main () { using first::x; using second::y cout << x << endl; cout << y << endl; cout << first::y << endl; cout << second::x << endl; return 0; } EXAMPLE OUTPUT: 5 2.7183 10 2.7183
  • 7. Notice how in this code, x (without any name qualifier) refers to first::x whereas y refers to second::y, exactly as our using declarations have specified. We still have access to first::y and second::x using their fully qualified names. The keyword using can also be used as a directive to introduce an entire namespace: // using example #include <iostream> using namespace std; namespace first { int x = 5; int y = 10; } namespace second { double x = 3.1416; double y = 2.7183; } int main () { using namespace first; cout << x << endl; cout << y << endl; cout << second::y << endl; cout << second::x << endl; return 0; }
  • 8. EXAMPLE OUTPUT: 5 10 3.1416 2.7183 In this case, since we have declared that we were using namespace first, all direct uses of x and y without name qualifiers were referring to their declarations in namespace first. using and using namespace have validity only in the same block in which they are stated or in the entire code if they are used directly in the global scope. For example, if we had the intention to first use the objects of one namespace and then those of another one, we could do something like:
  • 9. // using namespace example #include <iostream> using namespace std; namespace first { int x = 5; } namespace second { double x = 3.1416; } int main () { // Here are two blocks { using namespace first; cout << x << endl; } { using namespace second; cout << x << endl; } return 0; } EXAMPLE OUTPUT: 5 3.1416
  • 10. What Are Pointers? Reference: http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm  C++ pointers are easy and fun to learn.  Some C++ tasks are performed more easily with pointers, and  other C++ tasks, such as dynamic memory allocation, cannot be performed without them. As you know every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory. Consider the following which will print the address of the variables defined:#include <iostream> using namespace std; int main () { int var1; char var2[10]; cout << "Address of var1 variable: "; cout << &var1 << endl; cout << "Address of var2 variable: "; cout << &var2 << endl; return 0; } When the above code is compiled and executed, it produces result something as follows: Address of var1 variable: 0xbfebd5c0 Address of var2 variable: 0xbfebd5b6
  • 11. What Are Pointers?  A pointer is a variable whose value is the address of another variable.  Like any variable or constant, you must declare a pointer before you can work with it.  The general form of a pointer variable declaration is: type *variablename; Here, type is the pointer's base type; it must be a valid C++ type and variablename is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Following are the valid pointer declaration: int *ip; // pointer to an integer double *dp; // pointer to a double float *fp; // pointer to a float char *ch // pointer to character The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.
  • 12. There are few important operations Using Pointers in C++: Which we will do with the pointers very frequently: (a) We define pointer variables (b) Assign the address of a variable to a pointer and (c) Finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. Following example makes use of these operations: #include <iostream> using namespace std; int main () { int var = 20; // actual variable declaration. int *ip; // pointer variable ip = &var; // store address of var in pointer variable cout << "Value of var variable: "; cout << var << endl; // print the address stored in ip pointer variable cout << "Address stored in ip variable: "; cout << ip << endl; // access the value at the address available in pointer cout << "Value of *ip variable: "; cout << *ip << endl; return 0; }
  • 13. When the above code is compiled and executed, it produces result something as follows: Value of var variable: 20 Address stored in ip variable: 0xbfc601ac Value of *ip variable: 20 C++ Pointers in Detail: Pointers have many but easy concepts and they are very important to C++ programming. There are following few important pointer concepts which should be clear to a C++ programmer: Concept Description C++ Null Pointers C++ supports null pointer, which is a constant with a value of zero defined in several standard libraries. C++ pointer arithmetic There are four arithmetic operators that can be used on pointers: ++, --, +, - C++ pointers vs arrays There is a close relationship between pointers and arrays. Let us check how? C++ array of pointers You can define arrays to hold a number of pointers. C++ pointer to pointer C++ allows you to have pointer on a pointer and so on. Passing pointers to functions Passing an argument by reference or by address both enable the passed argument to be changed in the calling function by the called function. Return pointer from functions C++ allows a function to return a pointer to local variable, static variable and dynamically allocated memory as well.
  • 14. C++ NULL Pointers  It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned.  This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.  The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. Consider the following program: #include <iostream> using namespace std; int main () { int *ptr = NULL; cout << "The value of ptr is " << ptr ; return 0; } When the above code is compiled and executed, it produces following result: The value of ptr is 0 To check for a null pointer you can use an if statement as follows: if(ptr) // succeeds if p is not null if(!ptr) // succeeds if p is null
  • 15. C++ Pointer Arithmetics  As you understood pointer is an address which is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can a numeric value.  There are four arithmetic operators that can be used on pointers: ++, --, +, and - ptr++  the ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer .  This operation will move the pointer to next memory location without impacting actual value at the memory location.  If ptr points to a character whose address is 1000, then above operation will point to the location 1001 because next character will be available at 1001.
  • 16. Incrementing a Pointer:  We prefer using a pointer in our program instead of an array because the variable pointer can be incremented, unlike the array name which cannot be incremented because it is a constant pointer.  The following program increments the variable pointer to access each succeeding element of the array: #include <iostream> using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int *ptr; // let us have array address in pointer. ptr = var; for (int i = 0; i < MAX; i++) { cout << "Address of var[" << i << "] = "; cout << ptr << endl; cout << "Value of var[" << i << "] = "; cout << *ptr << endl; // point to the next location ptr++; } return 0; }
  • 17. Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in many cases. For example, a pointer that points to the beginning of an array can access that array by using either pointer arithmetic or array-style indexing. Consider the following program: #include <iostream> using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int *ptr; // let us have array address in pointer. ptr = var; for (int i = 0; i < MAX; i++) { cout << "Address of var[" << i << "] = "; cout << ptr << endl; cout << "Value of var[" << i << "] = "; cout << *ptr << endl; // point to the next location ptr++; } return 0; }
  • 18. When the above code is compiled and executed, it produces result something as follows: Address of var[0] = 0xbfa088b0 Value of var[0] = 10 Address of var[1] = 0xbfa088b4 Value of var[1] = 100 Address of var[2] = 0xbfa088b8 Value of var[2] = 200 MORE EXAMPLES #include <iostream> using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int *ptr[MAX]; for (int i = 0; i < MAX; i++) { ptr[i] = &var[i]; // assign the address of integer. } for (int i = 0; i < MAX; i++) { cout << "Value of var[" << i << "] = "; cout << *ptr[i] << endl; } return 0; } When the above code is compiled and executed, it produces following result: Value of var[0] = 10 Value of var[1] = 100 Value of var[2] = 200 You can also use an array of pointers to character to store a list of strings as follows: #include <iostream>
  • 19. using namespace std; const int MAX = 4; int main () { char *names[MAX] = { "Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali", }; for (int i = 0; i < MAX; i++) { cout << "Value of names[" << i << "] = "; cout << names[i] << endl; } return 0; } When the above code is compiled and executed, it produces following result: Value of names[0] = Zara Ali Value of names[1] = Hina Ali Value of names[2] = Nuha Ali Value of names[3] = Sara Ali