Image: Pointers in C
Image: Pointers in C

The pointers in C language refer to the variables that hold the addresses of different variables of similar data types. We use pointers to access the memory of the said variable and then manipulate their addresses in a program. The pointers are very distinctive features in C- it provides the language with flexibility and power.

What are Pointers in C?

The pointers perform the function of storing the addresses of other variables in the program. These variables could be of any type- char, int, function, array, or other pointers. The pointer sizes depend on their architecture. But, keep in mind that the size of a pointer in the 32-bit architecture is 2 bytes. Do see the Bootcamp coding courses at Edureify.

Let us look at an example where we define a pointer storing an integer’s address in a program.

int x = 10;
int* p = &x;
Here, the variable p is of pointer type, and it is pointing towards the address of the x variable, which is of the integer type.

How Do We Use Pointers in C?

Consider that we are declaring a variable “a” of int type, and it will store a value of zero.

int a = 0

Now, a is equal to zero.

Declaration of a Pointer

Just like the variables, we also have to declare the pointers in C before we use them in any program. We can name these pointers anything that we like, as long as they abide by the naming rules of the C language. Normally, the declaration of a pointer would take a form like this: data_type * name_of_pointer_variable; Note that here, The data_type refers to this pointer’s base type in the variable of C. It indicates which type of variable is the pointer pointing to in the code. The asterisk ( * ) is used to declare a pointer. It is an indirection operator, and it is the same asterisk that we use in multiplication. We can declare pointers in the C language with the use of the asterisk symbol ( * ). It is also called the indirection pointer, as we use it for dereferencing any pointer. Here is how we use it: int *q; // a pointer q for int data type char *x; // a pointer x for char data type. Also refer to the C language, JAVA, R language, Go language, etc.

The Initialization of a Pointer

Once we declare a pointer, we then initialize it just like the standard variables using the address of the variable. In case we don’t initialize the pointers in any C program and start using it directly, the results can be pretty unpredictable and potentially disastrous. We use the & (ampersand) operator to get the variable’s address in the program. We place the & just before that variable’s name whose address we require. Here is the syntax that we use for the initialization of a pointer. Check out the Bootcamp coding courses at Edureify. Syntax of Pointer Initialization pointer = &variable; Let us look at an example of how we can use pointers for printing the addresses along with the value. As we can assess in the figure shown above, the pointer variable is storing the digit variable’s address, named fff4. This digit variable’s value is equal to 100. But aaa3 is the pointer variable’s address (variable r). Download the Eduriefy app now for more information. Here, we can print the r pointer variable’s value using the indirection pointer ( * ). We will look at an example for the same, as explained in the figure mentioned above.

#include<stdio.h>
int main(){
int digit=100;
int *r;
r=&digit; // for storing the address of the digit variable in the program
printf(“The address of the variable r is equal to %x \n”,r); // pointer r contains the digit variable’s address, and thus, printing the variable r gives us the address of the digit.
printf(“The value of the variable r is equal to %d \n”,*r); // We are using * to dereference the pointer, and thus, printing *r, would give the value that is stored at that address which is contained by r.
return 0;
}

The output of the code mentioned above would be like this:

The address of the variable r is equal to fff4

The value of the variable r is equal to 100

Use of Pointers in C

Here is a summary of how we use the following operators for the pointers in any program:

*           Asterisk         Returns the referenced variable’s value.

&          Ampersand      Returns a variable’s address Swipe left

To know more about the C language, read this article.

The Pointer to an Array

Here is an example to illustrate the same,

int arr [20];

int *q [20] = &arr; // The q variable of the pointer type is pointing towards the integer array’s address or the address of arr.

The Pointer to a Function

Here is an example to illustrate the same,

void display (int);
void(*q)(int) = &show; // The q pointer is pointing towards the function’s address in the program

The Pointer to a Structure

Here is an example to illustrate the same,

struct str {
int x;
float y;
}ref;
struct str *q = &ref;

Types of Pointers

There are various types of pointers that we can use in the C language. Let us take a look at the most important ones.

The Null Pointer

The null pointer has a value of 0. To create a null pointer in C, we assign the pointer with a null value during its declaration in the program. This type of method is especially useful when the pointer has no address assigned to it. Let us take a look at a program that illustrates how we use the null pointer:

#include <stdio.h>
int main()
{
int *a = NULL; // the null pointer declaration
printf(“Value of the variable a in the program is equal to :\n%x”,a);
return 0;
}

The output obtained out of the program mentioned above will be:

The value of the variable a in the program is equal to 0

The Void Pointer

The void pointer is also known as the generic pointer in the C language. This pointer has no standard data type, and we create it with the use of the keyword void. The void pointer is generally used for the storage of any variable’s address. Let us take a look at a program that illustrates how we use the void pointer in a C program:

#include <stdio.h>
int main()
{
void *q = NULL; // the void pointer of the program
printf(“Size of the void pointer in the program is equal to : %d\n”,sizeof(q));
return 0;
}

The output obtained out of the program mentioned above will be:

The size of the void pointer in the program is equal to 4

The Wild Pointer

We can call a pointer a wild pointer if we haven’t initialized it with anything. Such wild pointers are not very efficient in a program, as they may ultimately point towards some memory location that is unknown to us. It will ultimately lead to some problems in the program, and then, it will crash. Thus, you must be very careful when using wild pointers in a program. Let us take a look at a program that illustrates how we use the wild pointer in a C program. Also, check the Bootcamp coding courses at Edureify.

#include <stdio.h>
int main()
{
int *w; // the wild pointer in the program
printf(“\n%d”,*w);
return 0;
}

The output obtained out of the program mentioned above will be a compile-time error.

Here are a few more types of pointers that you can find in the C programs:

  • Near pointer
  • Complex pointer
  • Huge pointer
  • Far pointer
  • Dangling pointer

Accessing Pointers- Indirectly and Directly

There are basically two ways in which a program can access a variable content and then manipulate it:

  • Direct Access: In this case, we use the variable name in the program directly.
  • Indirect Access: In this case, we use some pointers to the variables in the program.

Let us take a look at a program to understand this better. Consider the below program:

#include <stdio.h>
/* Declaration and initialization of an int variable in the program */
int variable = 1;
/* Declaration of the pointer to the int variable */
int *ptr;
int main( void )
{
/* Initialization of ptr to the point to the variable */
ptr = &variable;
/* Accessing var indirectly and directly */
printf(“\nThe direct access of the variable would be = %d”, variable);
printf(“\nThe indirect access of the variable would be = %d”, *ptr);
/* Displaying the address of the variable in both the ways */
printf(“\n\nOutput of the address of the variable would be = %d”, &variable);
printf(“\nOutput of the address of the variable would be = %d\n”, ptr);
/*changing of the content of the variable through the ptr pointer in the program*/
*ptr=48;
printf(“\nThe indirect access of the variable would be = %d”, *ptr);
return 0;
}

The compilation of this program devoid of errors would generate an output like this:

The direct access of the variable would be = 1

The indirect access of the variable would be = 1

The output of the address of the variable would be = 4202496

The output of the address of the variable would be = 4202496

The indirect access of the variable would be = 48

Pros of using Pointers in C

  • Pointers make it easy for us to access locations of memory.
  • They provide an efficient way in which we can access the elements present in the structure of an array.
  • We can use pointers for dynamic allocation and deallocation of memory.
  • These are also used to create complex data structures, like the linked list, tree, graph, etc.
  • It reduces the code and thus improves the overall performance. We can use it to retrieve the strings, trees, and many more. We can also use it with structures, arrays, and functions.
  • We can use the pointers for returning various values from any given function.
  • One can easily access any location of memory in the computer using the pointers.

Cons of Pointers in C

  • The concept of pointers is a bit tricky to understand.
  • These can lead to some errors like segmentation faults, accessing of unrequired memory locations, and many more.
  • A pointer may cause corruption of memory if we provide it with an incorrect value.
  • Pointers in a program can lead to leakage of memory.
  • As compared to all the other variables, pointers are somewhat slower.
  • Some programmers might find it very tricky to deal with pointers in a program. It is their responsibility to use these pointers and manipulate them carefully.

Applications of Pointers in C

There are various uses of the pointers in C language. Let us look at some of them:

  1. Dynamic allocation of memory: We can allocate the memory dynamically in the C language when we use the calloc() and malloc() functions. The pointers are primarily used in such cases.
  2. Structures, Functions, and Arrays: We generally use the pointers in the C language in the cases of structures, functions, and arrays. Here, the pointers help in reducing the code and improving the program’s performance.

The & Address of Operator in C

This operator basically returns the variable’s address. But keep in mind that we must use the %u if we want to display the variable’s address.

#include<stdio.h>
int main(){
int digit=100;
printf(“The value of the digit is equal to %d and the address of the digit is equal to %u”,digit,&digit);
return 0;
}

The output of the program mentioned above would be like this:

The value of the digit is equal to 100 and the address of the digit is equal to fff4

Edureify’s online coding courses aim to provide the best technical and coding courses to its students. Edureify’s coding courses offer courses in-

Frequently Asked Questions (FAQs)

Q:- What are a pointer and its types?

A:- A pointer is nothing but a memory location where data is stored. A pointer is used to access the memory location. There are various types of pointers such as a null pointer, wild pointer, void pointer, and other types pointers. Pointers can be used with arrays and strings to access elements more efficiently.

Q:- What are the types of pointers in C?

A:- There are majorly four types of pointers, they are:

  • Null Pointer.
  • Void Pointer.
  • Wild Pointer.
  • Dangling Pointer.

Q:- Why pointer is used in C?

A:- C uses pointers to create dynamic data structures — data structures built up from blocks of memory allocated from the heap at run-time. C uses pointers to handle variable parameters passed to functions. Pointers in C provide an alternative way to access information stored in arrays.

Q:- WHAT IS NULL pointer in C?

A:- A null pointer is a pointer that points to nothing. Some uses of the null pointer are: a) To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet. b) To pass a null pointer to a function argument when we don’t want to pass any valid memory address.

Facebook Comments