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

  • Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members. An object is an instanti…
  •   Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C++, this takes the form of a function that calls its…
  •   Constructor A constructor is a special kind of class member function that is executed when an object of that class is instantiated. Constructors are typically used to init…
  • You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of argu…
  • Inheritance is one of the key feature of object-oriented programming including C++ which allows user to create a new class(derived class) from a existing class(base class). The…
  • A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for f…

Post a Comment