Understand Pointers in C programming in easy way – 1

Pointers in C programming

In simple terms pointers are the variables which store addresses of another variable.
It can also store addresses of functions, arrays, structures and other pointers along with primitive data type variables.
But the question arises “why do we need pointers?“.
Let’s find out the answer for the above question.

Declaration of pointers

Declaration of pointer variables is almost same as declaring normal variable, only difference is that we add ‘* (asterisk)‘ sign before the pointer variable.
Syntax
data_type *pointer_variable_name;
E.g.
int *num; // pointer variable which can store address of int data type variable.
char *a; // pointer variable which can store address of char data type variable.

Size of the pointer is dependent on the architecture.

C Pointers

pointers-in-c-itvoyagers

Assigning value to pointer

Pointer accepts the address of another variable, so the first question we must ask is “how to get an address of any variable?”.
This is where “address unary operator” comes into the picture, ‘& (ampersand)’ is used to get the address of variables.

Now we can store this address in the pointer variable.

Syntax
data_type *pointer_name = &another_variable;

E.g.
int a = 8, b = 12;
int *ptr = &a;
int *ptr2;
ptr2 = &b;

Print value of pointer

To get an address stored in a pointer variable we have to write the name of the pointer variable as shown in below example.
To access the value which is stored in that particular address (which is stored in pointer variable) we have to use ‘* (asterisk)’ sign before the pointer variable as shown in the below example.

In the below example ptr is a pointer variable which stores the address of int variable x.
When we write “ptr” in a printf statement we are printing the value which is stored in ptr i.e. address of variable x we can print address in decimal and hexadecimal format as well.
When we write “*ptr” in printf statement we are printing the actual value which is stored on that particular address (which is stored in pointer variable) i.e. 10 which is the value of variable x.

 

#include <stdio.h>
int main()
{
int x = 10;
int *ptr;
ptr = &x; //address of x is stored in ptr
// This will print the address in decimal format which is stored in ptr
printf("nptr = %d",ptr);

// This will print the address in hexadecimal format which is stored in ptr
printf("nptr = %x",ptr);

// This will print the value which is stored on that address which stored in ptr
printf("n*ptr = %d",*ptr);
return 0;
}

Output :

ptr = 6422312
ptr = 61ff28
*ptr = 10

What is the NULL pointer?

If we don’t have to assign any value to the pointer at the time of declaration then we can assign “NULL” value to the pointer.
Although it is not compulsory or there is no rule to do this but it is a convenient or good practice to do so.

E.g.
int *ptr = NULL;

If we print the value of the above pointer then we will get 0 in output.
Most OS do not allow to access address 0 because it is reserved for OS.
Bus address 0 has special significance because it states that the pointer is not pointing towards accessible memory location.

Swap two numbers using Pointers

#include<stdio.h>
//itvoyagers.in
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
//itvoyagers.in
int main()
{
int x, y;
printf("Enter first number : ");
scanf("%d",&x);
printf("Enter second number : ");
scanf("%d",&y);
printf("Value of X before swapping = %dnValue of Y before swapping = %d", x, y);
swap(&x, &y);
printf("nValue of X after swapping = %dnValue of Y after swapping = %d", x, y);
return 0;
}

Output :

Enter first number : 5
Enter second number : 8
Value of X before swapping = 5
Value of Y before swapping = 8
Value of X after swapping = 8
Value of Y after swapping = 5

Advantages of pointers

  • With the help of pointers we can return multiple values from the function.
  • Pointers help to reduce the code.
  • We can access the memory location using pointers.

CHECKOUT OTHER RELATED TOPICS

CHECKOUT OTHER QUIZZES

PRACTICALS IN C PROGRAM

Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.
Practice Programs
1 – C program for leap year, easy and simple code
2 – Best C program to find sum of all digits
3 – Best C program to check Palindrome Number
4 – Best C program to check prime number
5 – Best C program to print Fibonacci series
6 – Best C program to reverse a number
7 – Best C program to reverse an array
8 – Best C program to find average of array
9 – Best C program to find frequency of array element
10 – Best C program to sort array in ascending order
11 – Best C program to find factorial using recursion
12 – Best C program to find sum of all numbers in array
13 – Best C program to find largest element in array
14 – Best C program to swap two numbers without third variable
15 – C Program to check if a number is even or odd
16 – C program to find Simple interest
17 – Best C program to add two matrices
18 – Best C program to count vowels and consonants in a string
19 – Best C program to find LCM
20 – Best C program to find HCF
21 – Best C program to swap two numbers using pointers
22 – Best C program to add two numbers using pointers
23 – Best C program to print Multiplication Table
24 – Best C program to check Armstrong number
25 – Best C program to print half pyramid of star(*)
26 – Best C program to print half pyramid of numbers
27 – Best C program to print reverse half pyramid of star
28 – Best C program to print reverse half pyramid of number
More coming soon…
We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment