Pointers in C is a special type of variable in C programming which deals with the memory directly.

What is Pointers in C?

Pointers in c used to deal with the memory addresses using programming techniques.
Definition of Pointers in C: A pointer is a variable which stores address of another variable.
In C programming basics we learned the concept of a simple variable which is used to store different type of data such as int, float, char, etc.
A pointer is another feature provided by c which holds the address of the variable.
Pointers deal with the memory addresses.

Declaring pointer variable: –

The syntax for declaring a pointer
<Datatype> *pointer-name;

In the above syntax, a data type of the pointer should be same as whose address of variable stored. we can write any type of data type for pointers.
And pointer name is the name of the pointer variable, * indicates it’s pointer variable.

Example:-
int *ptr;  => pointer variable

Accessing value of the pointer variable:

We can access the value of a variable by using the variable name or by the address of variable. we can access the value by using a pointer as it stores the address of the variable.
& Address of operator gives the address of the variable.
* gives the value at that address stored in a pointer variable.

Pointers Example :-
int *x=10;
int *ptr_var;
ptr_var = &var;

Memory representation of pointers:

Pointers in C
Pointers in C

For example: –
var = 10; value of var
ptr_var = 1000 => which gives value stored at 2000
*ptr_val = 10 => which gives value at address 1000

Array of pointers:

Array is a collection of similar elements. It stored elements in continuous memory location.
An array is a pointer which stores the address of its base address that is the first location of the array.
Notation a[i] expanded as *(a+i) to access individual elements of array .

The array of pointers Example:-

int var[]={1,2,3,4};
int *ptr_var[4];

Memory representation for an array of pointers: –

Array of pointers
Array of pointers

Pointer of string

We know that string is an array of characters. The array contains a base address that is string is actually a pointer.

The pointer of string Example: –

char var[20]=”Defineloop”;
int ptr_var = &var;

Memory representation: –

Pointer of string
Pointer of string

Pointer to pointer

The pointer is a variable which stores address of another variable.
To store the address of one pointer in another is referred as pointer to pointer.

Syntax for pointer to pointer: –

<Datatype> ** pointername;

Pointer to pointer Example:-

int num=10;
int *p1,**p2;
p1 = &num;
p2 = &p1;

Memory representation for pointer to pointer: –

Pointer to pointer
Pointer to pointer

Pointer to a structure

Structure in c is a collection of elements of different type which is stored in a single type.
In c programming, we can also assign the pointer to structure.
Pointer variable also stores the base address of the structure.

The syntax for a pointer to structure:-

struct structure_name *pointer_name;

Example for pointer to structure:-

struct technical {
string name ;
string email;
};
struct technical *ptr;

Memory Representation for pointer to structure: –

Pointer to structure
Pointer to structure

Pointer to function

In previous tutorials we learned the concept of the function concept.
Functions in c: function is a block of code or set of statements in c.
c also provides the feature to add a pointer to the functions.
We can also assign a pointer to function.

The syntax for a pointer to function: –

return_type (*pointer) (datatype, datatype);

Example for pointer to structure: –

(void) (*p) (int,float);

Pointers programs in c

#include <stdio.h>

int main()
{
int x;

int *p; //Pointer Variable

printf("\nEnter The Number : ");

scanf("%d",&x);

p=&x; //Assigning Memory Address

printf("\nValues :%d ",*p);

printf("\nMemory Address :%u ",p);

// %u used because address is positive only

return 0;
}

Output:-

Pointers in C

Explanations for the program:

  • First, we declare one variable as int x.
  • Next, we take pointer variable of type integer which is declared as int *p;
  • Now next we assign address of the variable x to pointer variable p as pointer variable stores the address of another variable.
  • As we learned pointers we know * used to access data stored at the address and & gives address. so according to that we print the value of variable x and address of variable x.
  • Suppose value = 20 and it stored at memory address 65524. based on this x and *p gives the value stored at address => 20 , &x and p gives memory address => 65524.
  • Print values of x and memory address.

Write A Comment