I prepared this slides for the student of FSC BSC BS Computer Science Students. These Slides are very easy to read and understand the pointer logic used in C++ Programming.
All Topic related to pointer is discussed and examples are given
1 of 21
More Related Content
Used of Pointer in C++ Programming
1. POINTER
U N I T 7
A U T H O R : A B D U L L A H J A N
2. MEMORY & REFERENCES
• Computer memory is a collection of different consecutive memory location. These memory location
are numbered sequentially. Each variable is created at a unique location in memory known as its
address.
• A program may declare many variable for different tasks. A variable declaration reserve specific
amount of space in memory for a particular variable. The variable name is used to refer to that
memory location. It allows the user to access a value in the memory. The computer refer to the
memory using an address. A variable declaration associates following three attributes to a variable:
• Variable name
• Variable type
• Variable memory address
• The following statement declares an integer variable:
• Int a;
• The name of variable is “a” and the type of variable is int. the address of the variable is unknown.
Computer create the variable at any available location in the memory. The memory address is a
hexadecimal number that refers to a particular location in the memory. The variable is created in the
memory as follow:
3. MEMORY & REFERENCES
• The above box represent the memory location allocated for the variable a. the hexadecimal value
above the box is its assumed address. The variable will occupy 2 bytes or 4 bytes in memory
depending on the type of computer.
• The actual address of the variable can be display by using reference operator &. It is also known as
address operator. The reference operator is used to access the memory address of a variable. The
following statement will display the address of a .
• Cout<<&a;
int
a
0*0064df0
4. MEMORY & REFERENCES
• #include<iostream>
• Using namespace std;
• Int main(){
• Int n = 10;
• Cout<<“the value of n:”<<n<<endl;
• Cout<<“the address of n “<<&n<<endl;
• Getch();
• }
Ouptut:
The value of n:10
The address of n:
0*8fb1fff4
5. POINTERS
• A pointer is a variable that is used to store a memory address. The reference operator
is used to access the memory address of a variable and store it in a pointer.
6. POINTER
• A pointer is a variable that holds the address of another variable. In memory each and
every variable has an address assigned to it by the compiler and if a programmer
wants to access that address another variable called pointer is needed. For the
declaration of pointer an asterisk* symbol is used between the data type and the
variable name.
• Consider the following pointers declaration of the integer variable marks, floating
point variable percentage and character type variable name
• Int *mark;
• Float *percentage;
• Char *name;
7. MEMORY ADDRESS
• When writing a program, variable need to be declared. Declaration of variable is simple
and its declaration tells the computer to reserve space in memory for this variable. The
name of the variable refers to that memory space. This task is automatically performed
by the operating system during runtime. When variable are declared programmers
store data in these variable. Computer refers to that space using an address. Therefore
everything a programmer declares has an address. It is analogous(same face) to the
home address of some person. Using pointer variable one can easily find out the
address of a particular variable.
8. REFERENCE OPERATOR &
• As pointers are the variable which hold the addresses of other variable therefore while
assigning addresses to them a programmer needs a special type of operator called
address-of operator that is denoted by ampersand & symbol. This provides address of
a memory location.
• To understand it consider the following segment of code
• Float x = 55;
• Float *fpointer;
• fPointer = &x; //assign address of x to pointer
1000 5.5
xfpointer
9. • Consider the following program
• Int main(){
• Float x = 55;
• Float *fPointer;
• fPointer = &x;
• Cout<<“the address of x = “<<&x<<endl;
• Cout<<“the value of x = “<<x<<endl;
• Cout<<“the value of fPointer =“<<fpointer;
• Getch();
• Return 0;
• }
• Ouptut :
• The address of x = 1000
• The value of x = 5.5
• The value of fPointer = 1000
• This program gives some other output (address) on another computer depending upon the
availability of the memory
10. DEREFERENCE OPERATOR*
• Pointer are used to store the address of another variable. If we want to store the value
of the variable through the pointer, then we need a special type of operator called
dereference operator denoted by asterisk*.
11. • Use of dereference (*) in a program
• Int main(){
• Int n = 100;
• Int *pn; //define a pointer to n
• Pn = &n; //pn store the address of n
• Int valueN;
• ValueN = *Pn;
• Cout<<“the address of n = “<<&n<<endl;
• Cout<<“the value of n = “<<n<<endl;
• Cout<<“the value of Pn = “<<Pn<<endl;
• Cout<<“the value of *Pn = “<<(*pn)<<endl;
• Cout<<“the value of value = “<<valueN;
• Getch();
• Return 0;
• }
12. • Output :
• The address of n = 0*8fc5fff4
• The value of n = 100
• The value of pn = 0*8fc5fff4
• The value of (*pn) = 100
• The value of valueN = 100
13. DECLARING VARIABLE OF POINTER
TYPE
• The declaration of pointer is simple and is similar to the declaration of a regular
variable with a minor difference of the use of an asterisk * symbol between the
datatype and the variable name.
• Consider the following general format
• Datatype * name variable;
• Example:
• Int* totalmarks;
• Char *name;
• Float * percentage;
14. VOID POINTER
• Usually the type of a variable and the type of pointer variable that holds memory address of the variable
must be the same. But the “void” type pointer variable can hold memory address of variable of any data
type. A void type pointer is declared by using the keyword “void” the asterisk is used before pointer variable
name.
• The keyword ‘void’ is used as the data type of the pointer as follows
• Void *p;
• The above statement declares a pointer variable p. the data type of the pointer is void. It means that it can
store the memory address of any type of variable.
• Int x;
• Float y;
• Void *pointervoid;
• Pointervoid = &x; // valid
• Pointervoid = &y; //valid
15. • In this segment of code pointerVoid store the address of both integer variable X and
floating point variable Y , the compiler decides at run time that the address of which
type of variable should be assigned to this pointer.
16. VOID POINTER
• Int main(){
• Int n= 10;
• Float f = 25.18;
• Char c = ‘$’;
• Void *ptr;
• Ptr = &n;
• Cout<<“the value of n:”<<n<<endl;
• Cout<<“the address of n:”<<ptr<<endl;
• Ptr = &f;
• Cout<<“the value of f:<<f<<endl;
• Cout<<“the address of f:”<<ptr<<endl;
• Ptr = &c;
• Cout<<“the value of c:”<<c<<endl;
• Cout<<“the address of c”<<ptr<<endl;
• Getch();
• }
Output
The value of n: 10
The address of n: 0*8f72
The value of f: 25.13
The address of f:0*f573
The value of c:$
The address of c: 0*f567
17. VOID POINTER
• The above program declares and initializes three variable of type int , float and char. It
also declare a void pointer variable. The pointer can refer to different types of variables
. The program stores the memory addresses of the variables n,f and c one and displays
the values and memory addresses of these variables
18. POINTER INITIALIZATION
• Assigning values to pointer at declaration time is called pointer initialization.
• Example
• Float temperature;
• Float *Ptemperaturre = &temperature;
• Or
• Float temperature;
• Float *Ptemperaturre;
• Ptemperature = &temperature;
19. POINTER INITIALIZATION
• It should be considered that at the moment of declaring a pointer, the asterisk *
indicates that it is a pointer variable and not the dereference operator.
20. POINTER INITIALIZATION
• Consider the following program to explain the concept of pointer initialization
• Int main(){
• Float temperature;
• Float *Ptemperature = &temperature;
• Cout<<“the address of temperature is =“<<&temperature<<endl;
• Cout<<“the value of *Ptemperature is = “<<Ptemperature<<endl;
• Getch();
• Return 0;
• }
21. POINTER INITIALIZATION
• Here the pointer Ptemperature is initialized with the address of the variable
temperature.
• Sometime we need to initialize a pointer to zero. Such pointers are called null pointers
and they do not point to anything. Null pointer can be defined by assigning address 0
to them.
• Consider the following initialization to demonstrate null pointer
• Int *NPtr; // define null pointer
• NPtr = 0; // this assigns address 0 to NPtr
• the use of Null pointers is mostly done in dynamic memory allocation