Pointers in c programming with examples and output – Pointers in C Programming Language

Pointers in c programming with examples and output: A pointer in C programming language is a variable which is used to store the address of another variable. It is one of the most powerful features of the C programming language. Pointers are used everywhere in the C language. Once you master the use of pointers, you will use them everywhere to make the code more efficient and faster.

Some operations in C, can be performed efficiently using pointers like dynamic memory allocation, dynamic data structures like linked list and trees, pass arguments to a function as Call by Reference, access array elements etc.

To become an expert C programmer, it is necessary to have deep understanding of pointers.

What Are Pointers

A variable in C is the name given to a memory location, where a program can store data. A program can store and manipulate the value stored using variable’s identifier. When we declare a variable in C, compiler reserve space in memory to hold the value assigned to this variable.

We can access the value of this variable either by variable identifier or by directly accessing the memory location using pointers. Every memory location is given a unique address, once you know the address of a memory location(variable), you’ll then be able to go to that address and retrieve the data stored in it. To get the address of any variable, we can use &(Address of) operator and to retrieve the value stored at a memory location we can use *(Value of Operator).

Advantages of Pointers

  • We can dynamically allocate or deallocate space in memory at run time by using pointers.
  • Using pointers we can return multiple values from a function.
  • We can pass arrays to a function as call by Reference.
  • Pointers are used to efficiently access array elements, as array elements are stored in adjacent memory locations. If we have a pointer pointing to a particular element of array, then we can get the address of next element by simply incrementing the pointer.
  • Pointers in C are used to efficiently implement dynamic Data Structures like Queues, Stacks, Linked Lists, Tress etc.
  • The use of pointers results into faster execution of program.

Pointer Operators

While handling a data through pointers, we need to know the address of a variable and second is given a memory address or pointer we want to access data stored at that location. Address of(&) and Value of(*) operators are used to perform above mentioned pointer operations respectively.

Address of Operator(&)
The & is a unary operator in C which returns the memory address of the passed operand. This is also known as address of operator. A pointer contains the memory address of some object.

Value of Operator(*)
The * is a unary operator which returns the value of object pointer by a pointer variable. It is known as value of operator. It is also used for declaring pointer variable.
For Example

    int A = 100;
    int *ptr = &A;

In the first statement, we first declare an integer variable and initialize it with value 100. In second statement, we are declaring a pointer to a variable of type int and initializing it with address of A.

Pointer Declaration

A pointer is a derived data type that is created from fundamental data types. We use (*) for defining pointer variables. Here is the syntax of declaring a pointer variable.

<data_type> *<identifier>;

For Example

    int *ptr;

Above pointer declaration specifies that, ptr is a pointer variable that will store the memory address where an integer value is stored.
We can also initialize a pointer as follows:

    int count = 10;
    int *ptr = &count;

Integer variable ‘count’ is the name to a memory location where value 10 is stored. Let the memory address of count is “52436721”. Then pointer variable ptr will contain address of count variable that is “52436721”

Pointers in C Programming Language

#include <stdio.h>
#include <conio.h>
 
int main () {
   int  count = 10;
   int *ptr = &count;
 
   printf("Value of count variable is %d\n", count);
   printf("Address of count variable: %x\n", ptr);
   printf("Value retrieved through pointer : %d\n", *ptr);
 
   getch();
   return 0;
}

Output

Value of count variable is 10
Address of count variable: 22fe44 
Value retrieved through pointer : 10