Learning About Pointers in C

Learning About Pointers in C

5 mins read951 Views Comment
Updated on Apr 11, 2023 17:55 IST

Pointers in C are variable that store the address of other variables instead of their value. Size of pointers vary according to their architecture. Many tasks in C programming cannot be performed without the use of pointers.

2022_09_Untitled-design-3.jpg

In C programming language, pointers are unique variables that serve as a memory address store for other variables. Pointers provide the C language with more flexibility. We will learn how to generate pointers and use them in C in this article.

Table of Contents

Addresses in C 

Before we learn about pointers, let’s learn about addresses in C programming.

Addresses serve as a reminder of where a variable is stored in memory, as we all know. Therefore, if we have declared a variable named var, the location of that variable in memory will be provided to us by &var. This is the reference operator and is given by the symbol &.

Let’s look at the following example code:

 
#include
\n \n \n <stdio.h>\n \n \n
\n \n \n
int main()\n \n \n
{\n \n \n
int var = 5;\n \n \n
printf("Value of var: %d\n", var);\n \n \n
\n \n \n
//Address of variable var\n \n \n
printf("Address of var: %p", &var); \n \n \n
return 0;\n \n \n
}\n \n \n
\n \n \n </stdio.h>
Copy code

Output:

 
Value of var: 5
Address of var: 0x7ffd89b72524
Copy code

What have we done here?

As you can see in this above-given example, the output represents the address in the hexadecimal form. 

You can assign addresses to pointers using the reference operator. Let’s see how this is done.

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
50 hours
– / –
40 hours
– / –
2 months
– / –
4 months
5.5 K
140 hours

Pointers in C

Every time a variable is declared in C programming, a specific location in memory is allotted for it to store its value. This location is known as the memory address of the variable.

A pointer in C is essentially a variable that stores the memory address of another variable. A pointer, as its name implies, points to the location of another variable. Pointers have a data type, just like regular variables, and it is the same as the variable it points to. The data type of a pointer tells us how many bytes of data are used by the variable whose address it stores.

Explore free C courses

Pointer Declaration in C

Syntax:

 
datatype *pointer_name;
// or
datatype* pointer_name;
// or
datatype * pointer_name;
Copy code

The asterisk * symbol is used for declaring a variable pointer_name as a pointer. The * symbol can be placed anywhere between the pointer name and the data type.

Let’s examine a brief illustration:

 
int* ptr1, *ptr2; //both ptr1 and ptr2 are pointers
int* ptr1, ptr2; // ptr1 is a pointer, whereas ptr2 is an int variable
Copy code

So, to declare multiple pointers together in the same line, use the * symbol before each pointer name. 

If-else Statement in C
Tokens in C Programming

Examples of C Pointers

Let’s look at a few examples to understand the working of pointers in C programming:

How to assign addresses to pointers?

We declare a pointer ptr and want it to point to the address of the variable x. Let’s see how we do it:

 
#include
\n \n \n <stdio.h>\n \n \n
\n \n \n
\n \n \n
int main()\n \n \n
{\n \n \n
\n \n \n
int* ptr, x;\n \n \n
x = 7;\n \n \n
ptr = &x;\n \n \n
\n \n \n
//Print value of x\n \n \n
printf("Value of x: %d", x);\n \n \n
\n \n \n
//Print address of x\n \n \n
printf("\nAddress of x: %p", ptr);\n \n \n
\n \n \n
return 0;\n \n \n
}\n \n \n
\n \n \n </stdio.h>
Copy code

Output:

 
Value of x: 7
Address of x: 0x7ffe96c7f31c
Copy code

Read C programming interview questions and answers

How to get the value of variable using a pointer?

Again, we declare a pointer ptr and want it to point to the address of the variable x. We can get the value of the variable x by using *ptr. 

Let’s look at how this is done using the above example:

 
#include
\n \n \n <stdio.h>\n \n \n
\n \n \n
\n \n \n
int main()\n \n \n
{\n \n \n
\n \n \n
int* ptr, x;\n \n \n
x = 7;\n \n \n
ptr = &x;\n \n \n
\n \n \n
//Print value of x\n \n \n
printf("Value of x: %d", *ptr);\n \n \n
\n \n \n
//Print address of x\n \n \n
printf("\nAddress of x: %p", ptr);\n \n \n
\n \n \n
return 0;\n \n \n
}\n \n \n
\n \n \n </stdio.h>
Copy code

Output:

 
Value of x: 7
Address of x: 0x7ffe96c7f31c
Copy code

How to change the value of the variables whose address the pointer points to?

Let us suppose the pointer ptr points to the address of the variable x. We can alter the value of the variable x by using *ptr, as shown in the example below:

 
#include
\n \n \n <stdio.h>\n \n \n
\n \n \n
\n \n \n
int main()\n \n \n
{\n \n \n
\n \n \n
int* ptr, x;\n \n \n
x = 90;\n \n \n
\n \n \n
//assign address of x to ptr\n \n \n
ptr = &x;\n \n \n
\n \n \n
//change value at address ptr\n \n \n
*ptr = 6;\n \n \n
\n \n \n
//Print new value of x\n \n \n
printf("Value of x: %d", x);\n \n \n
\n \n \n
return 0;\n \n \n
}\n \n \n
\n \n \n </stdio.h>
Copy code

Output:

 
Value of x: 6
Copy code

In the above example, the pointer ptr and &x have the same addresses. Hence, when *ptr is altered, it is reflected on the value of x.

Types of Pointers in C

Let us now discuss the different types of pointers in C in this section. 

1. NULL Pointers

In C programming, a pointer can likewise be given NULL (value = 0). Without actually storing any value in the pointer, NULL enables us to generate valid pointers. 

At address 0, the operating system reserves memory; most operating systems forbid programs from accessing memory at that address. The memory location 0 has a special significance.

It shows that the pointer is not intended to point to a memory location that is accessible. Dereferencing a NULL pointer causes undefined behavior; hence, if you attempt to access as *ptr (where ptr is a NULL pointer), a null pointer exception will be raised.

Let’s look at the following example:

 
#include
\n \n \n <iostream>\n \n \n
\n \n \n
using namespace std;\n \n \n
\n \n \n
int main () \n \n \n
{\n \n \n
// defining a null pointer\n \n \n
int *ptr = NULL;\n \n \n
\n \n \n
//Print value of ptr\n \n \n
printf("\nValue of the pointer ptr is: %p", ptr);\n \n \n
\n \n \n
return 0;\n \n \n
}\n \n \n
\n \n \n </iostream>
Copy code

Output:

 
Value of the pointer ptr is: (nil)
Copy code

2. Void Pointers

In C programming, the void pointer is a generic pointer that has no data type. The ability to typecast a void pointer to any type makes it useful for assigning different types of variables to the void pointer.

 
#include
\n \n \n <iostream>\n \n \n
\n \n \n
using namespace std;\n \n \n
\n \n \n
int main () \n \n \n
{\n \n \n
// defining a void pointer\n \n \n
void *ptr = NULL;\n \n \n
int var = 3;\n \n \n
\n \n \n
//Assign address of var to the pointer ptr\n \n \n
ptr = &var;\n \n \n
\n \n \n
//typecasted to int useing (int *)\n \n \n
printf("Value of *ptr is %d ", *(int *)ptr);\n \n \n
\n \n \n
return 0;\n \n \n
}\n \n \n
\n \n \n </iostream>
Copy code

Output:

 
Value of *ptr is 3
Copy code

3. Wild Pointers

In C programming, a wild pointer is a pointer that is not initialized to anything. A wild pointer has undefined behavior when dereferencing it, which could cause the program to crash or produce a garbage value.

Let’s look at the following example:

 
#include
\n \n \n <iostream>\n \n \n
\n \n \n
using namespace std;\n \n \n
\n \n \n
int main () \n \n \n
{\n \n \n
// defining a wild pointer\n \n \n
int *ptr;\n \n \n
printf("Value of *ptr is %d ",*ptr);\n \n \n
\n \n \n
return 0;\n \n \n
}\n \n \n
\n \n \n </iostream>
Copy code

Output:

 
Segmentation fault
Copy code

4. Dangling Pointers

In C programming, a pointer to a memory address that has been released or destroyed is referred to as a dangling pointer.

Let’s look at the following example:

 
#include
\n \n \n <stdlib.h>\n \n \n
\n \n \n
#include \n \n \n
\n \n \n <stdio.h>\n \n \n
\n \n \n
int main()\n \n \n
{ \n \n \n
//memory allocated to ptr\n \n \n
int *ptr = (int *) malloc(sizeof(int));\n \n \n
\n \n \n
// memory released\n \n \n
free(ptr);\n \n \n
\n \n \n
// ptr is pointing to a deleted memory location \n \n \n
// hence it is a dangling pointer\n \n \n
printf("Value of *ptr is %d ",*ptr);\n \n \n
\n \n \n
// Removing dangling pointer\n \n \n
ptr = NULL; \n \n \n
\n \n \n
return 0;\n \n \n
}\n \n \n
\n \n \n </stdio.h>\n \n \n
\n \n \n </stdlib.h>
Copy code

Output:

 
Value of *ptr is 0
Copy code

Endnotes

Hope this article has been helpful in understanding pointers in the C programming language. Explore our C articles to learn more about the language and consolidate your knowledge of the fundamentals.

______________________

Recently completed any professional course/certification from the market? Tell us what you liked or disliked in the course for more curated content.

Click here to submit its review with Shiksha Online

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio