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.

In this article, we will take a look at the Pointers in C and its uses according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to learn more.

Table of Contents

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.

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

The Initialization of a Pointer

Once we declare a pointer, we then initialise it just like the standard variables using an address of the variable. In case we don’t initialise 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 initialisation of a pointer,

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).

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:

Operator Name Uses and Meaning of Operator
* Asterisk Declares a pointer in a program.

Returns the referenced variable’s value.

& Ampersand Returns a variable’s address

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:

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:

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:

#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 of 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

Output of the address of the variable would be = 4202496

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 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

How to Read the Complex Pointers in C?

Several things are involved when a program reads complex types of pointers from a code in C. Let us take a look at the associativity and precedence of the operators that are used with the complex pointers to understand this better.

Operator Associativity Precedence
[] , () Left to Right 1
Identifier, * Right to Left 2
Data Type 3

Here, we can note a few things, like,

  • We use the bracket () operator to declare a function as well as define it.
  • The [] operator acts as a subscript operator for the arrays.
  • The * operator is just the pointer operator.
  • The identifier is a pointer’s name. It will always be assigned with priority.
  • The data type is also a type of variable. It is that type of variable to which any pointer intends to point in a program. The data type includes various modifiers such as long, int, etc.

Example

Let us take a look at an example where a program tries to read the pointer given below:

int (*s) [25]

If a program reads this pointer, it will see that both () and [] are present here. Now, both of these have an equal precedence. So we will consider their associativity here. Their associativity in a program is from left to right. Thus, the priority would go to the () operator.

Inside this bracket, the operator of the pointer * and the name of the pointer s (the identifier) also have the very same precedence. So, we have to consider their associativity here. The associativity is from right to left. Thus, the priority would go to s. The * will have the second priority.

The third priority will go to []. It is because this data type has the lowest precedence among all the others that we have discussed above. Thus, the pointer will look something like this:

  • char -> 4
  • * -> 2
  • s -> 1
  • [25] -> 3

The program will read this pointer as the s is a pointer to the array of integers of the size 25.

Practice Problems on Pointers in C

1. Which of these has higher precedence for pointers in a C program?

A. Brackets

B. Identifiers

C. Data Types

D. Asterisk

Answer: A. Brackets

2. What is the associativity for () and [] in a C program?

A. Right to Left

B. Left to Right

C. Equal

D. Depends on the precedence

Answer: B. Left to Right and D. Depends on the precedence


FAQs

Why do we use pointers in C?

We use pointers to access the memory of the said variable and then manipulate their addresses in a program. The pointers provide the language with flexibility and power. They store the addresses of other variables in the program. There are various uses of the pointers in C language. Let us look at some of them:

  • Dynamic allocation of memory: We can allocate the memory dynamically in C language when we use the calloc() and malloc() functions. The pointers are primarily used in such cases.
  • 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.

What is the void pointer in C?

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.

What are the types of pointers in C?

There are various types of pointers. Here are a few more:

  • Null Pointer
  • Void Pointer
  • Wild Pointer
  • Near pointer
  • Complex pointer
  • Huge pointer
  • Far pointer
  • Dangling pointer

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Leave a Comment

Your Mobile number and Email id will not be published.

*

*