Introduction to pointer in c

A pointers in c is a variable which holds the address of another variable. The variable can be of any data type.

The pointer in 64 bit architecture will have 8 bytes of memory and in 32 bit architecture it is 4 bytes.

Simple pointer variable can be created by using star (*) operator.

A pointer can be assigned an address of a variable by using & operator.

If you don’t want to assign the address of a variable to a pointer then you can dynamically allocate the memory and assign the address to the pointer.

If you pass pointer to a function that function can modify the variable. In case of pass by value if the function modifies the parameter value then the value in the calling function don’t be changed but in case of pass by Reference the original value get modified then value in the caller function.


How to declare pointers in c

As we already know a pointer can be declared by using star (*) operator.
The simple Syntax for declaring a pointer is data type followed by the star operator followed by the pointer variable name.

Look at the simple example below.

#include<stdio.h>
int main(int argc, char const *argv[])
{
	int variable=100;
	int *pointer=&variable;
	printf("The variable access through pointer is: %d\n",*(pointer));
	return 0;
}
Pointers in C

How to initialize a c pointer

First one is by simply using and operator and second one is allocating memory dynamically and assigning the address to a pointer variable.
Here we are using malloc function which is defined in stdlib header file.
Look at the example below.

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
	printf("\nHow to initialize a pointer\n");
	//method 1
	int variable=100;
	int *pointer=&variable;
	printf("The variable access through pointer is: %d\n",*(pointer));
	//Now lets allocate memory dynamically
	int *p=(int*)malloc(sizeof(int));
	*(p)=120;
	printf("The variable access through pointer is: %d\n",*(p));
	return 0;
}
How to initialize a c pointer
How to initialize a c pointer

Arithmetic operations on Pointers in C

You can also perform arithmetic operations on pointers in C.

Various arithmetic operations on C pointers is listed below:

  • Addition.
  • Subtraction.
  • Increment.
  • Decrement.
  • Comparison.

Pointer to an array in c

Pointer to the array can be created. Usually if you assign the the array to pointer the pointer will have the the memory address of the array you can simply access it by incrementing the pointer.

#include <stdio.h>
int main(int argc, char const *argv[])
{
	int array[5]={1,2,3,4,5};
	int *array_pointer=array;
	printf("Accessing array elements using pointer to array\n");
	for( int i=0 ;i<5  ; i++ )
	{
		printf("%d, ",(*array_pointer));
		array_pointer++;
	}
	return 0;
}

Pointer as function parameter

We can pass a pointer as function parameter.

Passing a pointer variable in the parameter is also known as pass by reference.

Keep in mind that if function modify the value then the value in the caller function also gets modified.

Look at the example below.

#include<stdio.h>
#include<stdlib.h>
void my_function(int *p)
{
		printf("The variable access through pointer via a function parameter is: %d\n",*(p));
}
int main(int argc, char const *argv[])
{
	printf("\nPointer as function parameter\n");
	int variable=100;
	int *pointer=&variable;
	my_function(pointer);
	return 0;
}
Pointer as  function parameter
Pointer as function parameter

Pointer as return value

You can return a pointer from a function. You have to specify the star operator after the return type in the function prototype.

The variable you are returning from a function is to be static. Because the variable you have created inside a function has the local scope, outside the function you cannot access the variable if you are returning that memory address, after the function goes out of the scope then the memory allocated by that function on the heap also gets destroyed hence you have to declare the pointer as static incase you are returning the pointer as return value.

Look at the example below to return a value from a function.

#include<stdio.h>
#include<stdlib.h>
int* my_function()
{
	static int my_var=100;
	//here iam directly returning the value using & operator.
	return &my_var;
}
int main(int argc, char const *argv[])
{
	printf("\nPointer as function parameter\n");
	int *pointer=my_function();
	printf("Return value from a function: %d\n",*(pointer));
	return 0;
}
Pointer as return value
Pointer as return value

Pointer to structure

You can also have a pointer to structure.
In order to access the member of a structure through pointer we have to use Arrow operator. 2 point can be assigned with the address of a structure either by using ampersand symbol (&) or allocating memory dynamically and assigning its address to the pointer.
Here we are using malloc function which is defined in stdlib header file.

Look at the example below to know how to pointer to structure.

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
	//structure variable
	struct my_struct
	{
		int data;
	};
	//creating a  pointer and allocating memory to it
	struct my_struct *struct_pointer=( struct my_struct * )malloc( sizeof(struct my_struct) );
	struct_pointer->data=100;//Accessign the structure member
	printf("Structure variable value: %d\n",struct_pointer->data);
	return 0;
}
Pointer to structure
Pointer to structure

Null pointers in c

A pointer can have null value that pointer is known as null pointer.

If you try to access the null pointer then abrupt termination may occur. In case of Unix based systems it will show the error as segmentation fault. Windows system some garbage value.

You can also assign a pointer with built in constant NULL to make it a null pointer.

A good programming practice involves in nullifying a pointer after its use. You can also use free function define in stdlib header.

Look at the example below to know about null pointer.

#include<stdio.h>
int main(int argc, char const *argv[])
{
	int *p=NULL;
	if( p==NULL )
	{
	 printf("Pointer is null\n");
	}
	else
	{
		printf("Pointer is not null\n");
	}
	
	return 0;
}
Null pointers in c
Null pointers in c

Practice more in C programming