DATA STRUCTURE AND ALGORITHM (Pointer)

 POINTER


Pointers are symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures.

    Syntax:
datatype *var_name; 
int *ptr;   //ptr can point to an address which holds int data

How to use a pointer?

  • Define a pointer variable
  • Assigning the address of a variable to a pointer using unary operator (&) which returns the address of that variable.
  • Accessing the value stored in the address using unary operator (*) which returns the value of the variable located at the address specified by its operand.

pointers in c



C++ program to illustrate Pointers in C++

#include <iostream>
using namespace std;
void pointer()
{
int var = 20;


int *ptr;    
//declare pointer variable

//note that data type of ptr and var must be same
ptr = &var; // assign the address of a variable to a pointer
cout << "Value at ptr = " << ptr << "\n";
cout << "Value at var = " << var << "\n";
cout << "Value at *ptr = " << *ptr << "\n";
}
//Driver program
int main()
{
pointer();
}


Output:
Value at ptr = 5c7fffdd95ggg
Value at var = 20
Value at *ptr = 20


Pointer Expressions and Pointer Arithmetic

A limited set of arithmetic operations can be performed on pointers which are:

  • incremented ( ++ )
  • decremented ( — )
  • an integer may be added to a pointer ( + or += )
  • an integer may be subtracted from a pointer ( – or -= )
  • difference between two pointers (p1-p2)

(Note: Pointer arithmetic is meaningless unless performed on an array.)




C++ program to illustrate Pointer Arithmetic in C++


#include <iostream>
using namespace std;
void pointer()
{
//Declare an array
int v[3] = {10, 100, 200};

//declare pointer variable
int *ptr;

//Assign the address of v[0] to ptr
ptr = v;

for (int i = 0; i < 3; i++)
{
cout << "Value at ptr = " << ptr << "\n";
cout << "Value at *ptr = " << *ptr << "\n";

// Increment pointer ptr by 1
ptr++;
}
} //Driver program
int main()
{
pointer();
}


Output:

    Value at ptr = 0x7fff9a9e7920
    Value at *ptr = 10
    Value at ptr = 0x7fff9a9e7924
    Value at *ptr = 100
    Value at ptr = 0x7fff9a9e7928
    Value at *ptr = 200

Untitled presentation (3)



Invalid pointers

A pointer should point to a valid address but not necessarily to valid elements (like for arrays). These are called invalid pointers. Uninitialized pointers are also invalid pointers.

int *ptr1;
int arr[10];
int *ptr2 = arr+20;

Here, ptr1 is uninitialized so it becomes an invalid pointer and ptr2 is out of bounds of arr so it also becomes an invalid pointer.
(Note: invalid pointers do not necessarily raise compile errors)



NULL Pointers

Null pointer is a pointer which point nowhere and not just an invalid address.
Following are 2 methods to assign a pointer as NULL;

int *ptr1 = 0;
int *ptr2 = NULL;




Connect with the Author:







Comments

Popular posts from this blog

DATA STRUCTURE AND ALGORITHM (Array)

DATA STRUCTURE AND ALGORITHM (Structure)