C – Pointers4 min read

In programming, there are various tasks that can be achieved easily with the use of pointers such as dynamic memory allocation that needs pointers to perform. It is important to learn pointers for the programmer because it minimizes the execution time and saves memory space. Before we jump into the Pointers let us understand a little bit about the Memory Addresses.

Memory Address:

As we already know at this point that every variables declared in a program are stored in computer storage possessing some memory address. And these addresses can be accessed by using the ampersand (&) operator. If you have the variable name var then &var is used to access the address of that variable in a program.

Consider the following program in C:

Output:


What is Pointer?

In C programming language pointer is a variable that stores or points to the address of another variable. This variable can be of type int, char, array, function, or any other pointer. It does not stores the value but only the address of the pointed variable.

Syntax of Pointer:

Pointer variables are declared in the following way using asterisk * symbol ( asterisk * = same asterisk used for multiplication).

You can declare the pointer with different C data-types too, some are:


Initialization of Pointers:

After declaring the pointer, we need to initialize the pointers. To get the actual address we use the ampersand (&) operator and it is used before the variable name that pointer needed to be the point.

Short example to use in a program:

  • Reference operator (&): It returns the variable’s address.
  • Dereference operator (*): It gets the value that has been stored in a memory address.

Size of Pointers in C.

The size of pointer is same for all the variable irrespective of their data types. However it depends on your machine architecture. For example, for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes.

See the example and you can check it on your computer by running the following program.

Output:


Example: Use of pointer in C Program.

Output:

As you can see, in the example above we printed two things:

  1. We printed ptr that had already stored the address of num (pointed by this line: ptr = #) and so the program displayed the address on num variable.
  2. We printed *ptr that displayed the actual value of the variable(num) that the address stored in ptr points to.

Now in a simple way, we can say that asterisk * returns the value of the referenced variable and ampersand (&) operator returns the address of a variable.


NULL Pointers

When a pointer is assigned with a null value at the time declaration then it is called a NULL pointer. We use this value when we do not assign any address to the pointer. The null pointer always contains a constant value of 0.

Let us see an C example for NULL pointer:

Output:


Advantages and Disadvantages of pointers

Pointers allow memory allocation that is Dynamic memory allocation as well as deallocation.
They are useful for complex data structures such as in a linked list, graph, tree, etc.

They are complex to understand, fault value may cause memory corruption pointers are comparatively slower than the variable.


MORE

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …