KEMBAR78
Pointers in Programming | PDF
Presented by:
HAMAD ZIA
ROLL NO.# 7102
BSSE-B
SEMESTER -2nd
TOPIC:
POINTERS
What is a Pointer?
❑ 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.
Pointer Declaration:
The method of declaring a pointer is same as declaring a simple
variable. An asterisk “*” is used in the declaration that indicates that the
variable is a pointer variable.
Syntax:
Data Type *var;
Example:
int *p;
A program to print memory address
 Output of program
 Address of i = 65520
 Value of i = 3
& used in this statement is ‘address of’ operator
Main()
{
int i=4;
cout<<“Adress of i = ”<<&i;
cout<<“Value of i = ”<<i;
}
* operator:
 The pointer operator available in C++ is *, called ‘value at address’
operator
 It gives the value stored at a particular address
 The ‘value at address’ operator is also called ‘indirection’ operator
Example program:
i 3
85065
Program output
Address of i = 85065
Value of i = 3
Value of i = 3* (&i) = *(85065) = 3
Pointer Initialization:
The process of assigning a memory address to a pointer at the time of
declaration is called pointer initialization. C++ does not initialize variables
automatically.
Syntax:
Data Type *P=&Variable;
Function Calls:
 Arguments can generally be passed to functions in one of the two
ways:
 sending the values of the arguments
 sending the addresses of the arguments
Example program 1 – function call by value
Memory
main()
change_value(
)
a = 10 b = 20
x = 20 y = 40
a = 10 b = 20
Press any key to continue …
10a 20b
6504 7505
10x 20y
3205 4350
20 40
Int a = 10, b = 20;
Cout<<a<<b;
Change_value(a,b);
Cout<<a,b;
Cout<<x,y;
Example program 1 – function call by address
Progrm Output
a = 10 b = 20
x = 20 y = 40
a = 20 b = 40
Press any key to continue
Memory
main()
change_value(
)
10a 20b
6504 7505
*x *y
3205 4350
*x
change_value(6505,
7505);
=*(6504
)
= 10
20 40
75056504
Array of Pointers
 An array of pointers is an array in which each element is a pointer. Each
element in the array can store a memory address. The array can store the
memory addresses of different objects of same type.
 Example:
Int main()
{
int *ptr [3],a, b, c, i;
ptr [0]=&a;
ptr [1]=&b;
ptr [2]=&c;
Cout<<“Enter 3 integers:”<<endl;
Cin>>a>>b>>c;
Cout<<“You entered the following values:”;
For(i=0;i<3;i++)
Cout<<*ptr [ i ] ;
}
Pointers in Programming

Pointers in Programming

  • 2.
    Presented by: HAMAD ZIA ROLLNO.# 7102 BSSE-B SEMESTER -2nd
  • 3.
  • 4.
    What is aPointer? ❑ 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.
  • 5.
    Pointer Declaration: The methodof declaring a pointer is same as declaring a simple variable. An asterisk “*” is used in the declaration that indicates that the variable is a pointer variable. Syntax: Data Type *var; Example: int *p;
  • 6.
    A program toprint memory address  Output of program  Address of i = 65520  Value of i = 3 & used in this statement is ‘address of’ operator Main() { int i=4; cout<<“Adress of i = ”<<&i; cout<<“Value of i = ”<<i; }
  • 7.
    * operator:  Thepointer operator available in C++ is *, called ‘value at address’ operator  It gives the value stored at a particular address  The ‘value at address’ operator is also called ‘indirection’ operator
  • 8.
    Example program: i 3 85065 Programoutput Address of i = 85065 Value of i = 3 Value of i = 3* (&i) = *(85065) = 3
  • 9.
    Pointer Initialization: The processof assigning a memory address to a pointer at the time of declaration is called pointer initialization. C++ does not initialize variables automatically. Syntax: Data Type *P=&Variable;
  • 10.
    Function Calls:  Argumentscan generally be passed to functions in one of the two ways:  sending the values of the arguments  sending the addresses of the arguments
  • 11.
    Example program 1– function call by value Memory main() change_value( ) a = 10 b = 20 x = 20 y = 40 a = 10 b = 20 Press any key to continue … 10a 20b 6504 7505 10x 20y 3205 4350 20 40 Int a = 10, b = 20; Cout<<a<<b; Change_value(a,b); Cout<<a,b; Cout<<x,y;
  • 12.
    Example program 1– function call by address Progrm Output a = 10 b = 20 x = 20 y = 40 a = 20 b = 40 Press any key to continue Memory main() change_value( ) 10a 20b 6504 7505 *x *y 3205 4350 *x change_value(6505, 7505); =*(6504 ) = 10 20 40 75056504
  • 13.
    Array of Pointers An array of pointers is an array in which each element is a pointer. Each element in the array can store a memory address. The array can store the memory addresses of different objects of same type.  Example: Int main() { int *ptr [3],a, b, c, i; ptr [0]=&a; ptr [1]=&b; ptr [2]=&c; Cout<<“Enter 3 integers:”<<endl; Cin>>a>>b>>c; Cout<<“You entered the following values:”; For(i=0;i<3;i++) Cout<<*ptr [ i ] ; }