Name – Amir hamza khan 
Instructor –Sir Irfan Latif Memon 
Section – BSCS-1 
RegNo. – 1312118
 Introduction to pointers. 
 The addresss of operator “&”. 
 Pointers and the “*” operator. 
 Pointers and function parameters. 
 Size of operators. 
Relation between array and pointers.
 The pointer is a variable that holds a memory address. 
 To understand the pointers we should first understand a bit 
about the computer memory. 
 Whenever any application is executed, it is loaded in to the 
memory form the disk and all the elements and components of 
that application will be located at the particular location 
somewhere in the memory.
 Computer memory is divided into sequentially numbered memory 
locations and each variable is located at a unique location in 
memory, known as its address. 
 The address of these memory locations is typically represented in 
Hexadecimal format with the prefix 0x before the number (e.g. 
0x8f4ffff2).
 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 
 To return the address of the certain variables the address-of 
operator is placed in front of the variable name as, 
 cout<<&var1; 
 cout<<&var2; 
 cout<<&var3; 
 Now the “&” used with the variables in the cout statement will 
return the addresses at which these variable are located into the 
memory.
 #include <iostream.h> 
 main () 
 { 
 int var1; 
 char var2[10]; 
 cout << "Address of var1 
variable: "; 
 cout << &var1 << endl; 
 cout << "Address of var2 
variable: "; 
 cout << &var2 << endl; 
 }
 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 *var-name; 
 Here, type is the pointer's base type; it must be a valid C++ type 
and var-name 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.
 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
 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
 #include <iostream.h> 
 main () 
 { 
 int *ptr = NULL; 
 Cout<<“the value of ptr 
is” <<ptr<<endl; 
 
 }
 Example: Function to swap 
the values of two variables 
 #include <iostream.h> 
 void swap2(int* a, int* b) 
 { 
 int tmp; 
 tmp = *a; 
 *a = *b; 
 *b = tmp; 
 return; 
 } 
 int main() 
 { 
 int x = 1, y = 2; 
 swap2(&x, &y); 
 cout<<x<<y; 
 return 0; 
 }
 Pointers can be used to pass addresses of variables to called 
functions, thus allowing the called function to alter the values 
stored there. 
 We looked earlier at a swap function that did not change the 
values stored in the main program because only the values were 
passed to the function swap. 
 This is known as "call by value".
 If instead of passing the values of the variables to the called 
function, we pass their addresses, so that the called function 
can change the values stored in the calling routine. This is 
known as "call by reference" since we are referencing the 
variables. 
 The following shows the swap function modified from a "call by 
value" to a "call by reference". Note that the values are now 
actually swapped when the control is returned to main 
function.
 The sizeof is a keyword, but it is a compile-time operator that 
determines the size, in bytes, of a variable or data type. 
 The sizeof operator can be used to get the size of classes, 
structures, unions and any other user defined data type. 
 The syntax of using sizeof is : 
 sizeof(data type) 
 The sizeof is a keyword, but it is a compile-time operator that 
determines the size, in bytes, of a variable or data type. 
 The sizeof operator can be used to get the size of classes, 
structures, unions and any other user defined data type.
#include <iostream.h> 
main() 
{ 
cout << "Size of char : " << sizeof(char) << endl; 
cout << "Size of int : " << sizeof(int) << endl; 
cout << "Size of short int : " << sizeof(short int) << endl; 
cout << "Size of long int : " << sizeof(long int) << endl; 
cout << "Size of float : " << sizeof(float) << endl; 
cout << "Size of double : " << sizeof(double) << endl; 
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; 
}
 Arrays and pointers are closely related 
◦ Array name is like constant pointer 
◦ Pointers can do array subscripting operations
 Accessing array elements with pointers 
◦ Assume declarations: 
int b[ 5 ]; 
int *bPtr; 
bPtr = b; 
◦ Element b[ n ] can be accessed by *( bPtr + n ) 
 Called pointer/offset notation 
◦ Addresses 
 &b[ 3 ] is same as bPtr + 3 
◦ Array name can be treated as pointer 
 b[ 3 ] is same as *( b + 3 ) 
◦ Pointers can be subscripted (pointer/subscript notation) 
 bPtr[ 3 ] is same as b[ 3 ]
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)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)

c++ pointers by Amir Hamza Khan (SZABISTIAN)

  • 2.
    Name – Amirhamza khan Instructor –Sir Irfan Latif Memon Section – BSCS-1 RegNo. – 1312118
  • 3.
     Introduction topointers.  The addresss of operator “&”.  Pointers and the “*” operator.  Pointers and function parameters.  Size of operators. Relation between array and pointers.
  • 4.
     The pointeris a variable that holds a memory address.  To understand the pointers we should first understand a bit about the computer memory.  Whenever any application is executed, it is loaded in to the memory form the disk and all the elements and components of that application will be located at the particular location somewhere in the memory.
  • 5.
     Computer memoryis divided into sequentially numbered memory locations and each variable is located at a unique location in memory, known as its address.  The address of these memory locations is typically represented in Hexadecimal format with the prefix 0x before the number (e.g. 0x8f4ffff2).
  • 6.
     As youknow 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  To return the address of the certain variables the address-of operator is placed in front of the variable name as,  cout<<&var1;  cout<<&var2;  cout<<&var3;  Now the “&” used with the variables in the cout statement will return the addresses at which these variable are located into the memory.
  • 7.
     #include <iostream.h>  main ()  {  int var1;  char var2[10];  cout << "Address of var1 variable: ";  cout << &var1 << endl;  cout << "Address of var2 variable: ";  cout << &var2 << endl;  }
  • 8.
     A pointeris 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 *var-name;  Here, type is the pointer's base type; it must be a valid C++ type and var-name 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.
  • 9.
     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
  • 10.
     It isalways 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
  • 11.
     #include <iostream.h>  main ()  {  int *ptr = NULL;  Cout<<“the value of ptr is” <<ptr<<endl;   }
  • 12.
     Example: Functionto swap the values of two variables  #include <iostream.h>  void swap2(int* a, int* b)  {  int tmp;  tmp = *a;  *a = *b;  *b = tmp;  return;  }  int main()  {  int x = 1, y = 2;  swap2(&x, &y);  cout<<x<<y;  return 0;  }
  • 13.
     Pointers canbe used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there.  We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to the function swap.  This is known as "call by value".
  • 14.
     If insteadof passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables.  The following shows the swap function modified from a "call by value" to a "call by reference". Note that the values are now actually swapped when the control is returned to main function.
  • 15.
     The sizeofis a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type.  The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.  The syntax of using sizeof is :  sizeof(data type)  The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type.  The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.
  • 16.
    #include <iostream.h> main() { cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; cout << "Size of short int : " << sizeof(short int) << endl; cout << "Size of long int : " << sizeof(long int) << endl; cout << "Size of float : " << sizeof(float) << endl; cout << "Size of double : " << sizeof(double) << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; }
  • 18.
     Arrays andpointers are closely related ◦ Array name is like constant pointer ◦ Pointers can do array subscripting operations
  • 19.
     Accessing arrayelements with pointers ◦ Assume declarations: int b[ 5 ]; int *bPtr; bPtr = b; ◦ Element b[ n ] can be accessed by *( bPtr + n )  Called pointer/offset notation ◦ Addresses  &b[ 3 ] is same as bPtr + 3 ◦ Array name can be treated as pointer  b[ 3 ] is same as *( b + 3 ) ◦ Pointers can be subscripted (pointer/subscript notation)  bPtr[ 3 ] is same as b[ 3 ]