Ad Code

Responsive Advertisement

Pointers in C++


What Are Pointers ?
pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is –

What is a pointer in programming?

·         In computer science, a pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
·         A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.
·         The unary or monadic operator & gives the "address of a variable''.
·         The indirection or dereference operator gives the "contents of an object pointed to by a pointer''.
The general form of a pointer variable declaration is –
Here, the type is the pointer's base type; it must be a valid C data type, and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement, the asterisk is being used to designate a variable as a pointer. Take a look at some of the valid pointer declarations –
In Brief:
Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like char, int, float, double or user defined data type like function, pointer, etc. or derived data type like an array, structure, union, enum.

Examples:
int *ptr;
int (*ptr)();
int (*ptr)[2];
Types of Pointers
·         NULL Pointer
·         Dangling Pointer
·         Generic Pointers
·         Wild Pointer
·         Complex Pointers
·         Near Pointer
·         Far Pointer
·         Huge Pointers

1. Null Pointer 

·         NULL Pointer is a pointer which is pointing to nothing.
·         NULL pointer points the base address of the segment.
·         In case, if you don’t have address to be assigned to pointer then you can simply use NULL
·         Pointer which is initialized with NULL value is considered as a NULL pointer.
·         NULL is macro constant defined in following header files –
stdio.h
alloc.h
mem.h
stddef.h
stdlib.h

Defining NULL Value

Visual Representation

Types of Pointers in C
Below are some of the variable representations of a NULL pointer.

Example of NULL Pointer

Output :

2. Dangling Pointer 

·         Dangling pointers arise when an object is deleted or de-allocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the de-allocated memory.
·         In short, pointer pointing to a non-existing memory location is called dangling pointer.

Examples of Dangling Pointer

There are different ways where Pointer acts as a dangling pointer.

Way 1 : Using free or de-allocating memory

We have declared the character pointer in the first step. After execution of some statements, we have the de-allocated memory which is allocated previously for the pointer.
As soon as memory is de-allocated for pointer, pointer becomes dangling pointer

Way 2 : Out of Scope

·         Character Pointer is Declared in the first Step.
·         Pointer Variable ‘ptr’ is pointing to Character Variable ‘ch’ declared in the inner block .
·         As character variable is non-visible in Outer Block, then Pointer is Still Pointing to Same Invalid memory location in Outer block, then Pointer becomes “Dangling”.

3. Generic Pointers

When a variable is declared as being a pointer to type void, it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer.
This is very useful when you want a pointer to point to data of different types at different times.

Example of Generic Pointer

Here is some code using a void pointer:

4. Wild Pointer

A Pointer in C that has not been initialized till its first use is known as the Wild pointer. A wild pointer points to some random memory location.

Example of Wild Pointer

How can we avoid Wild Pointers ?
We can initialize a pointer at the point of declaration wither by the address of some object/variable or by NULL;

5. Complex Pointers

Precedence : Operator precedence describes the order in which C reads expressions
Associativity : Order operators of equal precedence in an expression are applied
We need to assign the priority to the pointer declaration considering precedence and associative according to the following table.
Where
(): This operator behaves as bracket operator or function operator.
[]: This operator behaves as array subscription operator.
*: This operator behaves as pointer operator, not as the multiplication operator.
Identifier: It is not an operator, but it is name of the pointer variable. You will always find the
Priority will be assigned to the name of the pointer.
Data type: It is also not an operator. Data types also include modifier (like signed int,
long double etc.)

6. Near Pointer

·         The pointer which can points only 64KB data segment or segment number 8 is known as near pointer.
·         That is near pointer cannot access beyond the data segment like graphics video memory, text video memory, etc. Size of near pointer is two byte. With the help of keyword near, we can make any pointer as near pointer.

Example of Near Pointer

Output: 2

7. Far Pointer

·         The pointer which can point or access whole the residence memory of RAM, i.e., which can access all 16 segments is known as far pointer.
·         Size of the far pointer is 4 byte or 32 bit.

Example of Far Pointer

Output : 4

8. Huge Pointer

·         The pointer which can point or access whole the residence memory of RAM i.e. which can access all 16 segments is known as a huge pointer.
·         Size of the far pointer is 4 byte or 32 bit.

Example of Huge Pointer

Output : 4 4 1
Explanation:  p is the huge pointer, *p is the far pointer and **p is char type data variable.

Advantages of Pointers In C Programming

·          
·         We can dynamically allocate or deallocate space in memory at a run time by using pointers.
·         Using pointers, we can return multiple values from a function.
·         We can pass arrays to a function as call by Reference.
·         Pointers are used to efficiently access array elements, as array elements are stored in adjacent memory locations. If we have a pointer pointing to a particular element of an array, then we can get the address of next element by simply incrementing the pointer.
·         Pointers in C are used to efficiently implement dynamic Data Structures like Queues, Stacks, Linked Lists, Trees, etc.
·         The use of pointers results in the faster execution of the program.





Post a Comment

0 Comments

Close Menu