Pointer Definition in C

What are Pointers?

A pointer is a type of variable in the C language that is used to store the address of another variable. The pointers can be of any data type, such as int, float, char, string, array, or function. In C language, pointers are represented using the asterisk symbol (*). The size of the pointers depends on the architecture. For example, the size of the pointer will be 4 bytes for a 32-bit machine or 8 bytes for a 64-bit machine.

Features of pointers in C language

  • Pointers are used to save the memory of the computers.
  • It provides direct access to the memory address and thus saves time.
  • Pointers are used with data structures such as linked lists.
  • Dynamic allocation as well as de-allocation of memory is done using pointers.

Syntax for declaring a pointer in C language:

  • data_type * variable_name;

Array of pointers

Array of pointers in C language is the collection of pointers and is represented as follows:

  • data_type * variable_name[size];

In C language, arrays and pointers are related as an array name behaves like a pointer constant. Pointer constant's value will be the address of the first element. For example, let's take an array named ptr then ptr and &ptr[0] represents the same meaning and we can use both of them.

Assigning this value to a non-constant pointer to the array, we can access the elements of the array using this pointer.

Let's see an example for the array of pointers with int data type

Output:

Elements of the array are 1 2 3 4 5

Here is another example for array of pointers with string data type

Output:

The locations of the strings are:
4310680
4310684
4310688
4310692
4310696

'&' and '*' Operators:

'&' operator- When you assign a value to a variable, it is stored at a specific memory address. To access that memory address, we use the '&' operator. Thus, we can say that the '&' operator that is also known as the 'address of' operator or the reference operator, is used to give the memory address of a variable.

Pointer Definition in C

Output:

0x77fee2365f099 // memory address of the variable 'marks'. (Note: Output varies from computer to computer as memory address assigned by your computer will be different.)

Note: %p is a format specifier that is used to print the value of a pointer. Here &marks is a pointer.

'*' operator- '*' operator or the dereference operator is used to get the value of a variable at a given address. '*' operator basically has two functions. Using '*' while declaration, it is used to create a pointer, else it is used as a dereference operator. Here is an example that shows both the functions of '*' operator:

Pointer Definition in C

Output:

75

Types of Pointers in C language:

Pointer Definition in C

NULL Pointers:

Null Pointers are the pointers that have no value assigned to it. NULL pointers do not point to any function or object. It means that the pointer has not been assigned with any valid memory address.

Output:

The value inside the pointer ptr is:
0

Wild Pointers:

When you don't initialize anything to the pointer, the pointer is called as a wild pointer. This type of pointer may lead to the crashing of the program because the pointer will assign to a random memory address.

Void Pointers:

Void pointers are the pointers that do not have any specific data type. It is created by using the void keyword.

Dangling Pointers:

When you de-allocate the memory of a pointer using the free() function, then that pointer is known as dangling pointer.

Arithmetic Operations for Pointers in C language:

Not all but only few set of operations can be performed on pointers. The Pointer Arithmetic refers to the set of operations that can be performed on the pointers. It is different from the ones that we use for regular mathematical expressions. The operations are as follows:

  • Increment in a Pointer: Increment in a pointer refers to jumping of the pointer from one index to the very next index in an array.

It is represented as ptr++;

Output:

Value of *ptr = 20
Value of *ptr = 30
Value of *ptr = 40
  • Decrement in a Pointer: Decrement in a pointer refers to jumping from an index to the previous index of an array.

It is represented as ptr--;

Output:

Value of *ptr = 40
Value of *ptr = 30
Value of *ptr = 20
  • Adding a number to a pointer: Adding a number x to a pointer makes the pointer to increase by the x times the number of bytes of the data type of the variable.

Let's see an example:

Let's assume the memory address of val be 200, then the address of newval will be 208 instead of 202 that is, ptr+2* sizeof(int) where size of int is 4 bytes

  • Subtracting an integer from a pointer: Subtracting an integer from a pointer follows the same rule as mentioned above for addition.
  • Subtracting two pointers: Two pointers can only be subtracted if there data types are same. Subtraction of two pointers results in subtracting the memory address of the two pointers and then calculating the number of bits data according to the pointer data type. Subtraction of two pointers gives the increment between the two pointers.
    Let's take an example where an integer pointer ptr1 having memory address 100 and another integer pointer ptr2 having memory address 104 are to be subtracted. The difference between their memory address is 4. Since the size of int data type is 4, the result will be 4/4 that is equal to 1. Hence, the increment between the two pointers is 1.
  • Comparison of pointers: Comparison of two pointers is possible if they both have same data types. We can use all the relational operators supported in C language (<, >, <=, >=, !=, ==) and the result will be either true or false.

Output:

val2 is greater than val1

Advantages of Pointers:

  • Memory locations can be easily accessed with the help of pointers.
  • Elements of an array can be easily accessed with the help of pointers.
  • Pointers are used for dynamic memory allocation as well as de-allocation.
  • Pointers are used to create other data structures like linked list, graphs and trees.

Disadvantages of Pointers:

  • Pointers are a little difficult for the users to understand.
  • Since, pointers are little complex, they can cause errors like segmentation faults or they can access a memory location that is not necessary.
  • Pointers can cause leakage of memory.
  • An incorrect value given to a pointer may lead to corruption of memory.
  • Pointers are comparatively slower.
  • It is a difficult task for a programmer to manipulate a pointer.