C Programming

All Languages C CPP JAVA HTML CSS JAVASCRIPT PYTHON

Pointers In C

Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc.
Pointers are also variables and play a very important role in C programming language.

They are used for several reasons, such as:

Strings
Dynamic memory allocation
Sending function arguments by reference
Building complicated data structures
Pointing to functions
Building special data structures (i.e. Tree, Tries, etc...)
And many more.

What is a pointer?

A pointer is essentially a simple integer variable which holds a memory address that points to a value, instead of holding the actual value itself.

The computer's memory is a sequential store of data, and a pointer points to a specific part of the memory. Our program can use pointers in such a way that the pointers point to a large amount of memory - depending on how much we decide to read from that point on.

type *var-name;

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 -

int *ip;         /* pointer to an integer */ 
double *dp;      /* pointer to a double */ 
float *fp;       /* pointer to a float */ 
char *ch;         /* pointer to a character*/

The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.

How to use Pointers?

There are few important operations, which we will do with the help of pointers very frequently. a we define a pointer variable b assign the address of a variable to a pointer and c finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. Following

example makes use of these operations:

#include <stdio.h>
int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var );
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}

When the above code is compiled and executed, it produces result something as follows:

Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20

NULL Pointers in C

It is always a good practice to assign a NULL value to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.

The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following program:

#include<stdio.h>
int main ()
{
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr );
return 0;
}

When the above code is compiled and executed, it produces the following result:

The value of ptr is 0

On most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null zero value, it is assumed to point to nothing.

To check for a null pointer you can use an if statement as follows:

if(ptr) /* succeeds if p is not null */
if(!ptr) /* succeeds if p is null */

C Pointers in Detail:

Types Of Pointers In C Programming

* NULL Pointer
* Dangling Pointer
* Generic Pointers
* Wild Pointer
* Complex Pointers
* Near Pointer
* Far Pointer
* Huge Pointers

List Of Pointers In C Programming

1. Null Pointer

	-->stdio.h
	-->alloc.h	
	-->mem.h
	-->stddef.h
	-->stdlib.h

Defining NULL Value

#define NULL 0

Example

#include<stdio.h>
int main()
{
   int  *ptr = NULL;
   printf("The value of ptr is %u",ptr);

   return 0;
}

Output

The value of ptr is 0

2. 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

#include<stdlib.h>
{
    char *ptr = malloc(Constant_Value);
    .......
    .......
    .......
    free (ptr);      /* ptr now becomes a dangling pointer */
}

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

#include<stdlib.h>
void main()
 {
   char *ptr = NULL;
   .....
   .....
   {
       char ch;
       ptr = &ch;
   } 
   .....   /* dp is now a dangling pointer */
}

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:

int main()
{
  int i;
  char c;
  void *the_data;

  i = 6;
  c = 'a';

  the_data = &i;
  printf("the_data points to the integer value %d\n", *(int*) the_data);

  the_data = &c;
  printf("the_data now points to the character %c\n", *(char*) the_data);

  return 0;
}

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 ?

int main() 
{
  int  *ptr;
  /* Ptr is a wild pointer, as it is not initialized Yet */
  printf("%d", *ptr);
}

We can initialize a pointer at the point of declaration wither by the address of some object/variable or by NULL;

int main() 
{
  int val = 5;
  int  *ptr = &val; /* Initializing pointer */
  /* Ptr is not a wild pointer, it is pointing to the address of variable val */
  printf("%d", *ptr);
}

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.

operator precedence associative
(),[] 1 left to right
*,identifier 2 right to left
datab type 3 -

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

#include<stdio.h>
int main()
{
    int x=25;
    int near* ptr;
    ptr=&x;
    printf(“%d”,sizeof ptr);
    return 0;
}

Output is 2

7. Far Pointer

Example of Far Pointer

#include<stdio.h>
int main()
{
int x=10;
int far *ptr;
ptr=&x;
printf("%d",sizeof ptr);
return 0;
}

Output is 4

8. Huge Pointer

Example of Huge Pointer

#include<stdio.h>
int main()
{
char huge * far *p;
printf("%d %d %d",sizeof(p),sizeof(*p),sizeof(**p));
return 0;
}

Output is 441

Explanation:

p is the huge pointer, *p is the far pointer and **p is char type data variable.

Advantages of Pointers In C Programming