CrazyEngineers
  • Deepika

    Deepika

    MemberAug 16, 2011

    C++ Pointers-Concept Explained

    C++ pointer or simply a pointer is a simple variable that has the capability to store the memory address of some location.

    Lets discuss a small program for illustration:
    int * p;
    int q=10;
    p=&q;
    cout<<"Address of q is: "<
    • Here p is a pointer variable and q is a simple interger variable.
    • & is the address operator. So when we've the statement p=&q, we are actually initializing p with the memory address of q.
    • As memory addresses are generally of hexadecimal type, so p now also has some hexadecimal value.
    • p as such has no data type. But int *p means that the data type of the contents of memory location referenced by p (i.e. q) is interger.
    • So p is also called pointer to integer.
    • Displaying p will display the memory address of the variable q.
    • *p displays the content of the memory location it refers to i.e. value of q.
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Deepika Bansal

    MemberAug 16, 2011

    Lets consider the following as well:
    &*p is same as &(*p) which means address of the value stored.
    *&*p is same as *(&(*p)) which means value of variable through pointer p.
    *&p is same as *(&p) which means value of pointer.

    This means we always resolve such expressions from the inwards and moving outwards.
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberAug 16, 2011

    Pointer Expression
    int x=5,y=5;
    int *p, *q;
    int a,b,c,d;
    p=&x;               //p now contains the memory address of x ie p refers to x
    q=&y;              //q now contains the memory address of y ie q refers to y
    a=(*p)+(*q);   //x+y
    b=(*p)-(*q);   //x-y
    c=(*p)*(*q);   //x*y
     d=(*p)/(*q);   //x/y
    *p=*p+10;     // x=x+10
    
    Here we are accessing the value of x and y using the pointers p and q as *p and *q respectively.
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberAug 16, 2011

    Scale Factor
    We can also have something like p++ or p--.
    int *p;
    int x=10;
    p=&x;
    p++;
    
    Here when we are incrementing or decrementing the pointer variable p is according to the data type of the variable it is pointing to i.e. x in our case.
    In our example we are considering x to be of type int. Int type requires 2 bytes of memory location to store one integer value. So when we're having p++, we are actually moving two bytes so that p may now point to next integer value (which may be a garbage value as well).

    Had p be of type char *, p would have been incremeted by 1 byte to point to next char.
    Similarly for float *, p would have been incremented by 4 bytes to point to next float and so on.

    So the key formula is:
    ptr++ is same as ptr=ptr+1 which in turn is equivalent to ptr=ptr + (1*size of element).

    PS: Division and Multiplication is not applicable this way.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register