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

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More