Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link
Posts

4.3 Pointer

3 min read
Image result for Pointer C++ images
Pointer is a variable that stores the address of another variable. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data.

Pointer is used to allocate memory dynamically i.e. at run time. The variable might be any of the data type such as int, float, char, double, short etc.

Syntax :
To declare a pointer, we use an asterisk between the data type and the variable name
Pointers require a bit of new syntax because when you have a pointer, you need the ability to both request the memory location it stores and the value stored at that memory location.
data_type *ptr_name;

Example :
int *a; char *a;
Where, * is used to denote that ''a'' is pointer variable and not a normal variable.
In this context, the asterisk is not a multiplication

Key points to remember about pointers:
# Normal variable stores the value whereas pointer variable stores the address of the variable.
# The content of the pointer always be a whole number i.e. address.
# Always pointer is initialized to null, i.e. int *p = null.
# The value of null pointer is 0.
# & symbol is used to get the address of the variable.
# * symbol is used to get the value of the variable that the pointer is pointing to.
# If pointer is assigned to NULL, it means it is pointing to nothing.
# Two pointers can be subtracted to know how many elements are available between these two pointers.
# But, Pointer addition, multiplication, division are not allowed.
# The size of any pointer is 2 byte (for 16 bit compiler).

Since pointers only hold addresses, when we assign a value to a pointer, the value has to be an address. To get the address of a variable, we can use the address-of operator (&)

Example program for pointer:
#include
int main()
{
int *ptr, q;
&nbsp q = 50;

&nbsp /* address of q is assigned to ptr */
&nbsp ptr = &q;

&nbsp // prints address held in ptr, which is &q
&nbsp cout << ptr;

&nbsp /* display q's value using ptr variable */
&nbsp cout << *ptr;
&nbsp return 0;
}

The null pointer 

Sometimes we need to make our pointers point to nothing. This is called a null pointer. We assign a pointer a null value by setting it to address 0: int *ptr;
ptr = 0;
// assign address 0 to ptr
or simply
int *ptr = 0;
// assign address 0 to ptr

C++ Pointer Arithmetic 

As you understood pointer is an address which is a numeric value; therefore, you can perform arithmetic operations on a pointer just as you can a numeric value. There are four arithmetic operators that can be used on pointers: ++, --, +, and -.

Example :
ptr++;
ptr--;
ptr+21;
ptr-10;

If a char pointer pointing to address 100 is incremented (ptr++) then it will point to memory address 101

C++ Pointers vs Arrays 

Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in many cases. For example, a pointer that points to the beginning of an array can access that array by using either pointer arithmetic or array-style indexing.

int main ()
{
int var[3] = {1, 2, 3};
int *ptr;
cout << *ptr << endl;
ptr++;
cout << *ptr << endl;
return 0;
}

this code will return :
1
2

C++ Pointer to Pointer 

A pointer to a pointer is a form of multiple indirection or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value. int main ()
{
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var;
pptr = &ptr;
cout << "Value of var :" << var << endl;
cout << "Value available at *ptr :" << *ptr << endl;
cout << "Value available at **pptr :" << **pptr << endl;
return 0;
}

this code will return
Value of var :3000
Value available at *ptr :3000
Value available at **pptr :3000


                      4.4 Structure          NEXT >>

You may like these posts

  •   A switch statement is used instead of nested if...else statements. It is multiple branch decision statement of C++. A switch statement tests a variable with list of val…
  • The predefined object cerr is an instance of iostream class. The cerr object is said to be attached to the standard error device, which is also a display screen but the objec…
  •   while loop The while loop calculates the expression before every loop. If the expression is true then block of statements is executed, so it will not execute If the condi…
  • The predefined object clog is an instance of ostream class. The clog object is said to be attached to the standard error device, which is also a display screen but the object…
  • In most program environments, the standard input by default is the keyboard, and the C++ stream object defined to access it is cin. cin is an instance of iostream class For f…
  • if statement An if statement contains a Boolean expression and block of statements enclosed within braces. Structure of if statement if (boolean expression ) /* if expression …

Post a Comment