Pointers in C: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-05 03:44:44   14150  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Pointers are powerful features of C and (C++) programming that differentiates it from other popular programming languages like Java and Python. The pointer is a fundamental part of C. If you cannot use pointers properly then you have basically lost all the power and flexibility that C allows. The secret to C is in its use of pointers.

C uses pointers a lotWhy?:

  • It is the only way to express some computations.
  • It produces compact and efficient code.
  • It provides a very powerful tool.

C uses pointers explicitly with:

  • Arrays,
  • Structures,
  • Functions.

NOTE: Pointers are perhaps the most difficult part of C to understand. C's implementation is slightly different  in other languages. Pointers in C are difficult and fun to learn. Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect C programmer. Let's start learning them in simple and easy steps.

Address in C

Before you get into the concept of pointers, let's first get familiar with address in C.

If you have a variable var in your program, &var will give you its address in the memory, where & is commonly called the reference operator.

You must have seen this notation while using scanf() function. It was used in the function to store the user inputted value in the address of var.

scanf("%d", &var);

Program

/* Example to demonstrate use of reference operator in C programming. */
#include 
int main()
{
  int var = 5;
  printf("Value: %d\n", var);
  printf("Address: %u", &var);  //Notice, the ampersand(&) before var.
  return 0;
}

Output

Value: 5 
Address: 2686778

Note: You may obtain different value of address while using this code.

In above source code, value 5 is stored in the memory location 2686778. var is just the name given to that location.

What is Pointer

A pointer is a variable that holds a memory address. This address is the location of another object (typically another variable) in memory.

In another way we can say, A pointer is a variable whose value is the address of another variable, i.e., the direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.

Pointer Variables

If a variable is going to be a pointer, it must be declared as such. A pointer declaration consists of a base type, an *, and the variable name. The general form for declaring a pointer variable is

type *name;

where type is the base type of the pointer and may be any valid type. The name of the pointer variable is specified by name.

Reference operator (&) and Dereference operator (*)

As discussed, & is called reference operator. It gives you the address of a variable.

Likewise, there is another operator that gets you the value from the address, it is called a dereference operator (*).

Below example clearly demonstrates the use of pointers, reference operator and dereference operator.

Note: The * sign when declaring a pointer is not a dereference operator. It is just a similar notation that creates a pointer.

How to Use Pointers?

There are a few important operations, which we will do with the help of pointers very frequently. 

(a) We define a pointer variable,

 (b) assign the address of a variable to a pointer and

 (c) finally, access the value at the address available in the pointer variable.

pointer in c

This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. The following example makes use of these operations ?

Program

 #include 

int main () {

   int  var = 20;   /* actual variable declaration */
   int  *ip;        /* pointer variable declaration */

   ip = &var;  /* store address of var in pointer variable*/

   printf("Address of var variable: %x\n", &var  );

   /* address stored in pointer variable */
   printf("Address stored in ip variable: %x\n", ip );

   /* access the value using the pointer */
   printf("Value of *ip variable: %d\n", *ip );

   return 0;
}
 

When the above code is compiled and executed, it produces the following result ?

Output

 Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
 

Example To Demonstrate Working of Pointers

Program

/* Source code to demonstrate, handling of pointers in C program */
  #include 
int main(){
   int* pc;
   int c;
   c=22;
   printf("Address of c:%u\n",&c);
   printf("Value of c:%d\n\n",c);
   pc=&c;
   printf("Address of pointer pc:%u\n",pc);
   printf("Content of pointer pc:%d\n\n",*pc);
   c=11;
   printf("Address of pointer pc:%u\n",pc);
   printf("Content of pointer pc:%d\n\n",*pc);
   *pc=2;
   printf("Address of c:%u\n",&c);
   printf("Value of c:%d\n\n",c);
   return 0;
}
 

When the above code is compiled and executed, it produces the following result ?

Output

Address of c: 2686784
Value of c: 22

Address of pointer pc: 2686784
Content of pointer pc: 22

Address of pointer pc: 2686784
Content of pointer pc: 11

Address of c: 2686784
Value of c: 2
 

Key Points to remember about Pointer in C

  • Normal variable stores the value whereas pointer variable stores the address of the variable.
  • The content of the C pointer always be a whole number i.e. address.
  • Always C pointer is initialized to null, i.e. int *p = null.
  • The value of null pointer is 0.
  • & symbol is used to get the address of the variable.
  • * symbol is used to get the value of the variable that the pointer is pointing to.
  • If a pointer in C is assigned to NULL, it means it is pointing to nothing.
  • Two pointers can be subtracted to know how many elements are available between these two pointers.
  • But, Pointer addition, multiplication, division are not allowed.
  • The size of any pointer is 2 byte (for 16 bit compiler).
MCQ Available

There are 17 MCQs available for this topic.

17 MCQTake Quiz