Pointers in C and C++

Filed Under: C Programming

What is Pointer in C?

A pointer is a variable that holds the address of another variable to which it points. We know that if a variable is defined, it allocates some memory location. To know the address of that memory location, a pointer variable is used. Each data type has its own pointer variable. For example, a pointer to int, a pointer to double, a pointer to char, etc. A memory address is similar to house number or bank locker numbers.

Advantages of Pointers

  • Length and complexity of program can be reduced.
  • Searching/Sorting large size data is much easier.
  • Pointer are use for allocating and deallocating memory dynamically
  • Pointer increases the program’s performance.

Disadvantages of Pointers

When a pointer goes wrong, it is very difficult to find the bug in the program. If a pointer contains an incorrect value, it can lead to a disaster of great magnitude when used.

When reading a memory location by using this incorrect pointer, a user may be reading an incorrect or a garbage value. For example, consider a scenario in banking in which a customer’s real account is switched with this garbage value. In that case, the customer can become a millionaire or beggar in seconds.

The ‘*’ Operator and addressof(&) operator

The addressof(&) operator is used to find out the memory address of variable.


#include<bits/stdc++.h>
int main()
{
int x;
std::cout<<x<<"\n";
std::cout<<"the value of variable x is:"<<x<<"\n";
std::cout<<"The memory addressof a variable x is:"<<&x;
}

Address of Illustration

The ‘*’ operator is an indirection operator or dereferencing operator. It means the value at a particular address of a variable. It is another way of accessing a value stored at the memory address of a variable.


#include<bits/stdc++.h>
int main()
{
int a,b;
a=5;b=10;
std::cout<<"The vaule of variable a is:"<<a<<"\n";
std::cout<<"The address of variable a is:"<<&a<<"\n";
std::cout<<"The value of variable 'a' at address:"<<&a<<"is:"<<*(&a);
}

Star Operator Illustration

Pointer Variable

We know that pointer is a variable that holds the address of the variable to which it points.

Let us assume that y is a pointer variable that holds the memory address of a variable x, then it can be assigned by the statement as:


y = &x.
Creating Pointer Variable

Creating Pointer Variable

Here, y is pointing to variable x such that y contains the memory address of variable x. In other words, we can say that y is a pointer to variable x.

However, y has its own memory address in the system memory as it allocates some space in memory. It is to be noted here that the values 2738 and 1200 are assumed values for memory address.

Declaring a Pointer Variable


data_type*variable_name;

int*marks;, char*ptr1;, double*p1;

Initializing a Pointer Variable


pointer_variable = &variable_name

p = &a; //storing address of 'a' to pointer variable p.

Pointer Arithmetic

As we know that pointer is an address which is an integer value, various arithmetic operations can be performed on a pointer just as you can perform on the numeric value.

The various arithmetic operations are:

  • Any integer value can be added to pointer variable. It increments the pointer as built-in type variable does.
  • Any integer type value can be decremented from a pointer variable as a built-in data variable does.
  • A pointer can be incremented or decremented by using ++ and — urinary operator respectively.
  • Two pointer variable cannot be multiplied or added. However, the value of one pointer variable can be subtracted from the other pointer variable provided both pointer point to same variable.

Incrementing or Decrementing Pointer Variable


int a,*p1;
p1 = &a;
p1++; //value increased by 2 bytes
p1--; //value decreased by 2 bytes

char b,*p2;
p2 = &b;
p2++; // value decreased by 1 bytes
p2--; // value decreased by 1 bytes

float c,*p3;
p3 = &c;
p3++; // value decreased by 4 bytes
p3- -; // value decreased by 4 bytes

Pointers and Arrays

Each element of an array can be accessed easily and efficiently by using pointers.

For example, “int a[5]” shows that the variable a is an array that holds 5 elements of integer data type. By using pointer arithmetic, we can access each array element conveniently even in large and complex programs.

As discussed earlier, a pointer variable holds the very first memory address which is known as base address. In case of arrays, the name of the variable is itself base address. In fact, this base address is a pointer itself that points to the first element of array (at subscript 0).


int a[5];

Here, a is an array variable of integer data type as well as it stores the very first memory address known as a base address.

Now each array element can be accessed by using this base address and the incremented by 1 successively for accessing the next array element.

Pointers and Constants

Pointers can also point to a constant. It specifies that a pointer can read the value of a variable to which it points but cannot modify the value of that variable.


int x;
int y=20;
const int*ptr = &y; // pointer to integer constant
x = *ptr;           // valid. Reading ptr
*ptr = x;           // error! modifying ptr, which is a constant qualified

This Pointer

‘this’ pointer is an internal pointer that holds the memory address of a current object. ‘this’ pointer is a variable that is used to access the address of the class itself. This unique pointer is automatically passed to a member function when it is called. The pointer ‘this’ acts as an implicit argument to all the member function. ‘this’ keyword is used to represent an object that invokes the member function. ‘this’ pointer is very important when operators are overloaded.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages