Pointer in c

Pointer in c, is pointer variable which holds address of another variable, it means, it holds address as it value. Pointer makes accessing a normal variable faster because it knows which memory location has to be accessed. To work with pointer c provides two different operators

*Dereferencing operator
&AddressOf operator
Dereferencing operatorTo create a pointer variable and to get value of pointer variable.
AddressOf operatorUsed to get the address of a variable.

Declaring Pointer

To use a pointer in c program, it needs to be declared first.

Syntax

<data_type>* variable_name;

Example

int* age;  // Pointer to int
int *age;  // Pointer to int

Above both pointer declaration are same and valid c pointer declaration.

char *name; // Pointer to char

Initializing pointer or Assigning addresses to Pointer

Pointer holds the address of another variable, Hence, variable should be of same data type as pointer.

Syntax

<data_type>* pointer_variable_name;
<data_type> normal_variable_name =  value;
pointer_variable_name = &normal_variable_name;

Example

int* pntr; // Declaring pointer variable
int number = 30; // Declaring integer variable
pntr = &number;  // Assigning variable address to pointer
  1. To initialize a pointer variable, pointer variable without * is used in left hand side.
  2. To get address of variable, number & is used.
  3. Because pointer holds address so &number is assigned to pntr.
  4. Now pntr holds the address of variable number.

Get value from pointer in C

To get value from pointer variable like address and value of another variable different format specifiers are used. It is also called Dereference a Pointer.

#include<stdio.h>
void main()
{
int number = 30;
int*pntr;
pntr= &number;

printf("value of number using *pntr is %d\n" , *pntr);
printf("Address of number variable is %p\n" , &number);
printf("Address of number variable using *pntr is %p\n", pntr);
printf("Address of *pntr is %p\n", *pntr);

}

Format specifier used in pointer

Format specifier are used in c to get or output the type of data according to its data type.

%p with pointer variable *pntrReturns the address of pointer
%d with pointer variable *pntrReturns the value of variable which it holds.
%p with pntr without *Returns address of variable which it holds.
%p with &variable_nameReturns the address of variable itself.

Changing/Modify Value Pointed by Pointers

#include<stdio.h>
void main()
{
int number = 30;
int*pntr;
pntr= &number;

printf("Before changing value *pntr is %d\n" , *pntr);

*pntr = 200; //Changed value of pointer variable

printf("After changing value *pntr is %d\n" , *pntr);
}

Size of a Pointer Variable

Size of pointer variable in c is completely depends on system architecture it is running. As per architecture following are the pointer size.

System architecture in BitsPointer Size
32 bits4 bytes
64 bits8 bytes

Pointer variable size does not depends on the size of variable it holds.

Pointers Types in C

As per different use, there are different types of pointers available in c. They are

  1. Null Pointer
  2. Dangling Pointer
  3. Void Pointer
  4. Wild Pointer
  5. Near Pointer
  6. Far Pointer
  7. File Pointer
  8. Function Pointer
  9. Double Pointer
  10. Integer Pointer
  11. Float Pointer
  12. Structure Pointer

Null Pointers

It is a pointer with null value assigned. It means null pointer do not points to any address of variable.

Example

#include<stdio.h>
void main()
{
  int* pntr = NULL;
  printf("%d", *pntr);
}

NULL must be in capital letters.

Dangling Pointers

A pointer pointing to a variable memory address which is not available because it is freed or deleted.

Void Pointers

It a also called generic pointer because it can point to any type variable, and can be converted back to same data type.

Wild Pointer

This type of pointers are not been initialized, such uninitialized pointers are called wild pointers. Using these pointers in program could produce unwanted output and may crash program sometimes.

Near Pointer

It can hold only 16 bit addresses in a 16 bit machine. These pointer are not able to access memory outside of current segment. Near pointer address can be incremented or decremented. Size of far pointer is 2 bytes.

Syntax

<data_type> near *variable_name;

Example

int near *pntr;

Far Pointer

Far pointer is of 32 bits It can hold 32 bit address and capable of accessing memory outside of current segment. Size of far pointer is 4 bytes.

Syntax

<data_type> far *variable_name;

Example

int far *pntr;

File Pointer

It is a data type which allows to work and perform different operations related to file handling.

Function pointer

These pointer points to a function hence called function pointers.

Double Pointers

These pointer stores the address of another pointer variable called Double pointers and also called pointers to pointers.

Integer pointer

Pointer points to a integer variable memory address called integer pointer.

Float pointer

Pointer points to a float variable memory address called float pointer.

Char pointer

Pointer points to a char variable memory address called char pointer.

Array pointer

A Pointer which points to an array called array pointer or pointer to array.

Structure pointer

A pointer which stores address of structure or points to a structure called structure pointer.