Showing posts with label pointer. Show all posts
Showing posts with label pointer. Show all posts

Friday, January 25, 2013

Understand pointer to a pointer (**)

// program illustrating the pointer to pointer (**)
#include <iostream>
using namespace std;

int main()
{
    int i = 3;
    int *j;
    int **k;
   
    j = &i;
    k = &j;
   
    cout << "\n Address of i: " << &i << endl;
    cout << "\n Address of i: " << j << endl;
    cout << "\n Address of i: " << *k << endl;
    cout << "\n Address of j: " << &j << endl;
    cout << "\n Address of j: " << k << endl;
    cout << "\n Address of k: " << &k << endl;
   
    cout << "\n value of i: " << j << endl;
    cout << "\n value of k: " << k << endl;
    cout << "\n value of i: " << i << endl;
    cout << "\n value of i: " << *(&i) << endl;
    cout << "\n value of i: " << *j << endl;
    cout << "\n value of i: " << **k << endl;
    return 0;
}

OUTPUT:
Pointer terminology:
    int a = 12;
    int *b;
    b = &a;
   
    1. b contains address of an int
    2. value at address contained in b is an int
    3. b is an int pointer
    4. b points to an int
    5. b is a pointer which points in the direction of an int

-----------------------------------------------------------------------------------------------

// Function returning pointer
#include <iostream>
using namespace std;

int *fun();  //fun declaration

int main()
{
    int *p;
    p = fun();
    cout << "\n" << p << endl;
    cout << *p << endl;
    return 0;
}

int *fun()
{
    int i = 20;
    return(&i);
}

OUTPUT:


Saturday, December 15, 2012

A program for understanding 'Array of pointer'

#include <iostream>
#include <string.h>
using namespace std;

class city
{
    protected:
        int len;
        char*name;
       
    public:
        city()
        {
            len = 0;
            name = new char[len + 1];
        }
       
        void getname()
        {
            char *a;
            a = new char[30];
       
            cout << "\n Enter city name: ";
            cin >> a;
            len = strlen(a);
            name = new char[len + 1];
            strcpy(name, a);
        }
   
        void putname()
        {
            cout << "\n city name: " << name;
        }
};

int main()
{
    city *p[10];
    int n = 1;
    int opt;
   
    do
    {
        p[n] = new city;
        p[n++] -> getname();
        cout << "\n enter 1 for more entry of city name or 0 for exit: ";
        cin >> opt;
    }while(opt);
   
    cout << "\n\n";
    for (int i=1; i<=n; i++)

    {
        p[i] -> putname();

    }
}

Pointer with classes

Pointer with classes:
                           c++ supports dynamic binding which is one of the powerful features of object oriented programming. To achieve dynamic binding we require virtual functions, which is implemented by concept of pointers with classes and object. Here we discuss concept of pointer, after that we explain virtual function.

1.1 Pointer of objects:
                                Car * santro; (is a pointer to an object of class Car)
as it happened with datastructure, in order to refer directly to a member of an object pointed by a pointer we use the arrow operator (->) of indirection. here is an example with some possible combinations:

//Pointer to classes

#include <iostream>
using namespace std;

class CRectangle
{
    int width, height;

    public:
        void set_values (int, int);
        int area(void)
        {
            return (width * height);
        }
};

void CRectangle::set_values (int a, int b)
{
    width = a;
    height = b;
}

int main()
{
    CRectangle a, *b, *c;
    CRectangle *d = new CRectangle[2];
    b = new CRectangle;
    c = &a;
   
    a.set_values (1, 2);
    b->set_values (3, 4);
    d->set_values (5, 6);
    d[1].set_values (7, 8);
   
    cout << "\n      a area: " << a.area();
    cout << "\n    *b area: " << b->area();
    cout << "\n    *c area: " << c->area();
    cout << "\n d[0] area: " << d[0].area();
    cout << "\n d[1] area: " << d[1].area();
}

Wednesday, April 25, 2012

What is pointer

A pointer stores an address, so when you use the bare pointer, you get that address back. you have to add something extra, the asterisk(*), in order to retrieve or modify the value stored at the address.

A variable stores a value, so when you use the variable, you get its value. you have to add some extra, the ampersand(&), in order to retrieve of that variable.
  • Pointer contain an address, not data.
  • Normal variable -- storage location.
  • Pointer -- A variable that holds an address as its value.  
// simple pointer program
#include <iostream>
using namespace std;

int main()
{
    int x;        //A normal integer
    int *p_int;   // A pointer to an integer  (declaration of pointer)
   
    p_int = &x;    //Read it, "assign the address of x to p_int" (definition of pointer)
    cout << "Please enter a number: ";
    cin >> x;
   
    cout << *p_int << '\n';   //Note the use of the * to get the value
    *p_int = 10;
   
    cout << x;   // output 10 again!   
}

OUTPUT:
Please enter a number: 12
12
10

Note: pointer is a datatype.
  • Size of pointer datatype is dependent according to OS architecture.
  • In 32-bit system size of pointer datatype is 4 byte and in 64-bit it is 8 byte.
  • Byte is a datatype (size is 8 bit) (what is datatype of byte? ; byte to khud datatype hai)
Pointers are of pointer type. If you're asking about how pointer values are represented in memory, that really depends on the platform. They may be simple integral values (as in a flat memory model), or they may be structured values like a page number and an offset (for a segmented model), or they  may be something else entirely. 
-----------------------------------------------------------------------------------------------
// Interview Question
#include <iostream>
using namespace std;

int main()
{
    int x=5;
    int * p;
   
    *p = 8;
    cout << *p << endl;
   
    *p = x;   
   
    cout << *p << endl;
   
    *p = 6;
    cout << *p << endl;
   
    cout << x;
}

OUTPUT:
8
5
6
5


*p = x;  yeh pointer hi nahi bna,
*p = 6;  to x ki value 6 koi ho jayegi 

-----------------------------------------------------------------------------------------------
//For more light on pointer concept
#include <iostream>
using namespace std;

int main()
{
    int i=3;
    int *j;
   
    j = &i;
    cout << "\n Address of i (&i): " << &i << endl;
    cout << "\n  Address of i (j): "  << j << endl;
    cout << "\n Address of j (&j): "  << &j << endl;
    cout << "\n    value of j (j): "  << j << endl;
    cout << "\n    value of i (i): "  << i << endl;
    cout << "\nvalue of i [*(&i)]: "  << *(&i) << endl;
    cout << "\n   value of i (*j): "  << *j << endl;
    return 0;
}


OUTOUT: 

More on pointer

BY: Dheeraj Singh (Galgotia college)