POINTERS IN C

📚 Topics Covered

WHAT IS A POINTER?

DEFINITION:

A pointer is a special variable. It is a variable which is used to store the address of another variable.
You can think of a pointer as it is interlinked the variable. If anything changes inside the variable it will
be reflected in the pointer variable as well OR if you make any changes into the pointer it will make the changes in
the variable to which it is interlinked with.

real life example of a variable

In the above image, the representation of pointer is displayed in form of visuals.

SYNTAX: HOW TO CREATE A POINTER VARIABLE


datatype *pointerVariablename;
    

EXAMPLE:


int *pointer1;
    

SYNTAX: HOW TO STORE ADDRESS OF ANOTHER VARIABLE IN POINTER


datatype *pointerVariablename,variableName = value;

pointerVariablename = &variableName;
    

EXAMPLE:


int *pointer1,number1 = 10;

pointer1 = &number1;
    

EXAMPLE:


#include stdio.h

int main()
{
	int *pointer1,number1 = 5;
	pointer1 = &number1;

	printf("VALUE INSIDE NUMBER1 VARIABLE: %d\n",number1);
	printf("VALUE INSIDE POINTER1 POINTER VARIABLE: %d\n\n",*pointer1);

	number1 = 10;
	printf("VALUE INSIDE NUMBER1 VARIABLE: %d\n",number1);
	printf("VALUE INSIDE POINTER1 POINTER VARIABLE: %d\n\n",*pointer1);

	*pointer1 = 15;
	printf("VALUE INSIDE NUMBER1 VARIABLE: %d\n",number1);
	printf("VALUE INSIDE POINTER1 POINTER VARIABLE: %d\n\n\n",*pointer1);


	printf("VALUE INSIDE POINTER1 VARIABLE: %d\n\n",pointer1);

	return 0;
}


    

OUTPUT:


VALUE INSIDE NUMBER1 VARIABLE: 5
VALUE INSIDE POINTER1 POINTER VARIABLE: 5

VALUE INSIDE NUMBER1 VARIABLE: 10
VALUE INSIDE POINTER1 POINTER VARIABLE: 10

VALUE INSIDE NUMBER1 VARIABLE: 15
VALUE INSIDE POINTER1 POINTER VARIABLE: 15



VALUE INSIDE POINTER1 VARIABLE: -7325958947
    

In the above program if we write pointer1 instead of *pointer1 in the printf It will display the memory address of the number1 variable.

ARRAYS IN POINTER

WHAT IS HAPPENING IN ARRAYS IN POINTER?

In this topic we understand how an array is stored inside a pointer and how do we access the values of arrays through pointer.

EXAMPLE:


#include stdio.h

int main()
{
	int *pointer1,numbers[5] = {2,5,6,7,8},i;

	pointer1 = &numbers;

	for(i=0;i<5;i++)
	{
		printf("Value %d: %d\n",i+1,*(pointer1 + i));
	}

	return 0;

}
    

In the above example, we have stored the address of the array inside the pointer.

OUTPUT:


2
5
6
7
8
    

PASS POINTERS IN FUNCTION

HOW TO PASS POINTERS IN A FUNCTION?

In this topic we understand how an pointer is passed an argument in a function.

EXAMPLE:


#include stdio.h


void display(int *p)	//pointer as a parameter
{
	printf("VALUE INSIDE THE VARIABLE IS: %d\n",*p);
}

int main()
{
	int number1;

	printf("Enter a number: ");
	scanf("%d",&number1);

	display(&number1);

	return 0;

}
    

In the above example, we have passed address of number1 as an argument to the parameter *p.

OUTPUT:


Enter a number: 45
VALUE INSIDE THE VARIABLE IS: 45
    

Video Explanation

If video not loading,watch on YouTube

Practice Task

Try to understand the previous programming lessons to become perfect in Core Fundamentals of programming.

Challenge

Try to create your own mini project so that you will understand what you have truly learnt about C.