Concept of Pointers in C Language with Examples and Output

Concept of Pointers in C Language with Examples and Output

Today we are going to discuss a very important concept in C language ie. pointers in C language. You might be thinking that the concept of pointers in C language is very hard! But no. If you are perfect in all the previous concepts like array, structure, data type, operators, functions etc, then working with pointers is very easy. How easy? Let’s see.

What is a pointer?

What is a pointer‘ means just a pointer points to a memory location. Pointer’s concept is very very important. Without using pointers no programming language will be present in this world. But why? What is the reason?

The only reason is pointers use to access the information from a memory. If memory is not there, data processing is not there, accessing the data & storing the data is not there then what is the use of application and what is the use of programming languages? So, no use! Right? That is why the pointer is a very important concept.

Simply a pointer is used to access the information on a particular memory location.

How can we declare pointers in C language?

I think you already know how to declare a variable in C language! If not, then a simple declaration of a variable is as follows:

data-type     Identifier;

Example:

int    a;

Here the ‘int‘ is the data-type & ‘a‘ is the identifier. So, this is the syntax for declaring a variable. Now just convert this into a pointer declaration by simply adding a ‘*‘. Let’s see how.

The general declaration of a pointer variable is:

data-type*     Identifier;

or,

data-type     *Identifier;

Both of the above declarations are allowed in the C language. Either you can place the ‘*‘ in front of the variable or you can place it followed by the data-type.

Example:

int*    a;

int    *a;

How many types of pointers in C language?

Only two types of pointers in C lanhuage.

  1. Typed pointer.
  2. Untyped pointer.

Typed pointer:

The typed pointer points to a specific type of data. For example, if you take integer pointers (int), it will only point to an integer type of data. If it is a double pointer (double), it will only point to double data. Suppose it is a structure pointer like struct Emp*, it can point to only Employee data.

int* → integer data

double* → double data

struct Emp* →Employee data

Untyped Pointer:

The untyped pointer can point to any type of data. This is also called generic pointers in C language. It can be declared as void* which points to any type of data.

void* → Any type of data

So this two are the basically the types of pointers. Generally in pointer’s concept, whatever the operation you do we need to take the help of only two operators. Now, what are these two operators?

Operators used in pointer's concept:

The first operator is an address operator (&) and the second one is the pointer operator (*).

Address operator (&) returns address of a particular variable that means the memory location of the particular variable. One thing you should remember that in a programming language an address is always a positive integer value.

Pointer operator (*) returns the value inside a specified address.

Now let’s see all these stuff with an example.

#include<stdio.h>
#include<conio.h>
void main(){
   int i = 100;
   int* ptr;
   clrscr();
   ptr = &i;
   printf("%u", i);
   printf("%u", ptr);
   printf("%u", &i);
   printf("%u", &ptr);
   printf("%u", *ptr);
   printf("%u", *(&i));
   getch();
}
pointers in C language

What did we do in the above program?

  • Inside the main(), we have declared an integer variable and initialized it with a value of 100. So ‘i‘ get memory allocation at some memory location.
  • We have declared an integer pointer variable ‘ptr‘. Since we are working with integer data, that is why we have used integer pointer. So, ‘ptr‘ also get memory allocation at some location.
  • Now, ‘ptr = &i‘ means we want to store the address of ‘i‘ in the location of ‘ptr‘ variable.
  • Now the printing part. Here we are using %u instead of %d because %u always returns a positive integer. Since we are working with the memory location and memory location can’t be a negative value.
  • We are printing ‘i‘ which prints 100.
  • Next, we are printing ‘ptr‘. It returns the address of ‘i‘. From the output of the program, we know it is 65524.
  • Again we are printing ‘&i‘ that means the address of ‘i‘. It is again 65524 as usual.
  • Next, we are printing the address of ptr i.e. ‘&ptr‘. From the output, we know it is 65522.
  • Now we are printing the pointer to ‘ptr‘ i.e. ‘*ptr‘. Means, the value of the specified address. Here we have specified the address of ptr i.e. 65524. Now the pointer to 65524 means the data which is inside the memory location 65524. Since 100 is the data which is inside the 65524, so it will print 100.
  • At last, we are printing ‘*(&i)‘ means, the pointer to address of ‘i‘. Now, the address of ‘i‘ is 65524, so the pointer to address of ‘i‘ means value inside 65524 which is 100.
concept of pointer in C language example

This is a simple example of how to work with a pointer concept. There are so many concepts are there like how to work with the pointer to function, the pointer to string, call by value, call by reference etc. We will discuss these concepts in upcoming articles.

Liked this article? Then please like our Facebook Page & stay connected with us. 

Also Read: Basic Concept of Object Oriented Programming Language

Debarshi Das

Debarshi Das is a passionate blogger & full-stack JavaScript developer from Guwahati, Assam. He has a deep interest in robotics too. He holds a BSc degree in Information Technology & currently pursuing Masters of Computer Application (MCA) from a premier govt. engineering college. He is also certified as a chip-level computer hardware expert from an ISO certified institute.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close Menu
%d bloggers like this: