Search This Blog

Saturday, 1 December 2012

What are Pointers in C++? Don't get affraid of Pointers, these are easy!

Points to Remember:
  • *  is used to acces value of a pointer. It's called indirection or dereferencing operator.
  • & is used to get the address of a variable or a pointer. It's called address operator.
  • While declaring a pointer * indicates that the variable being declared is a pointer. 


How we use Pointers?
Ans:
#include<iostream>
using namespace std;
int main()
{
int *Ptr;   //Declaring a pointer named Ptr, you can change the name.
int a = 7;
Ptr = &a; //Pointers store addresses. Address of 'a' variable will be stored in the pointer.
cout<< *Ptr<<endl; //* means the value of Ptr where it's pointing
return 0;
}
//===============================================================================
//array_name gives the address of first location (index number 0) of array in hexadecimal system.
#include<iostream>
using namespace std;
int main()
{
int z[] = { 1, 2, 3, 4, 5 };
cout<<z;
return 0;
}
//===============================================================================
Both will have same output
But if we'll do something as below, then it's stupidity, because array_name gives the address of first location (index number 0) of array in hexadecimal system.

// *array_name gives the value stored at first location of array.
#include<iostream>
using namespace std;
int main()
{
int z[] = { 1, 2, 3, 4, 5 };
cout<<*z;
return 0;
}
//===============================================================================
// what if we want to access any other number stored in array?
#include<iostream>
using namespace std;
int main()
{
int z[] = { 1, 2, 3, 4, 5 };
cout<< *(z+4) << endl;// 5 is printed.
cout<< *z + 3 << endl;// = value of z at first location (1) + 3 => 4
return 0;
}
//===============================================================================
//You can access any index of array using pointers by using subscripts.
#include<iostream>
using namespace std;
int main()
{
int *Ptr;
int z[] = { 1, 2, 3, 4, 5 };
Ptr = z; // or You can write Ptr = &z[0];
cout<< Ptr[3]<<endl;
return 0;
}
//===============================================================================
//Pointer's pointing postion can be changed. Interesting!
#include<iostream>
using namespace std;
int main()
{
int *Ptr;
int z[] = { 1, 2, 3, 4, 5 };
Ptr = z; // or You can wrtie Ptr = &z[0];
Ptr++; // Now pointer starts pointing to the next integer stored in array i.e. 2
cout<< *Ptr <<endl;
return 0;
}
//===============================================================================
Subtracting pointers
Returns number of elements between two addresses
This means that p1_array is 3 ahead of p_array.
Lets move towards Void Pointers
We can't dereference a void pointer:
Example:
What if we want to dereference a void pointer???
We will have to change our style for this.
#include <iostream>using namespace std;
 int main()
{
 int x = 5;
 void *Voidptr; //declaring a void pointer, which will point to a variable of type void.
 Voidptr = &x;  //initializing void pointer with the address of x variable of int type.
//If we want to access x, then what will we do? Will we do cout << *Voidptr;          "NO"
//We cannot dereference a void pointer because it's pointing to a variable of type void.
//So, we cannot do cout << *Voidptr;
//Therefore we'll introduce another pointer (say ptr1) of type int which will point to x variable indirectly.
 int *ptr1 = static_cast<int*>(Voidptr); /*changing the type of pointer from void to int for one step and storing it permanently.*/
 cout<<*ptr1;
 return 0;
}


Feel free to ask if there is any problem?

3 comments:

  1. in
    #include
    using namespace std;
    int main()

    {
    int *Ptr;
    int z[] = { 1, 2, 3, 4, 5 };
    Ptr = z; // or You can write Ptr = &z[0];
    cout<< Ptr[3]<<endl;

    return 0;
    }

    in ptr=z;
    what is stored in ptr???

    ReplyDelete
  2. can u tell error in this code???
    ???????????????????????????

    #include
    using namespace std;
    int man()
    {
    int *ptr;

    int x[]={1,2,3,4,5};

    ptr=x;//can also b ptr=&x[0]
    cout<<ptr[4]<<endl;
    return 0;
    }
    ????????????????????????????

    ReplyDelete
  3. Answering Q.1: array name is the pointer to the first location of array, so ptr is storing the address of array's first location named 'z'.
    Pointers always store addresses.

    Answer to Q.2: int main() is the error, spelling mistake.

    ReplyDelete