For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
Before we go into the nitty-gritty, let's address the question, What Are Pointers in C?
Pointers are a type of variable that holds the memory address of another variable. This is a unique feature that sets C apart from many other languages. Pointers provide a direct way to access and manipulate memory, which can be both powerful and dangerous.
Understanding Pointers in C is crucial for writing efficient and effective code. Let’s delve into the syntax, use cases, advantages, and even the drawbacks of pointers for better comprehension.
The notation 0x123 is a way to represent numbers in hexadecimal (base 16) format in the C programming language. This hexadecimal number system is commonly used in programming and computer science because it's very efficient for defining binary data and manipulating bits.
The 0x prefix is used in C (and in many other programming languages) to denote that the number following it is a hexadecimal number. So 0x123 is a hexadecimal number.
The hexadecimal number system uses 16 different digits:
To convert 0x123 to a decimal number, we can calculate as follows:
Adding these results together gives us 256 + 32 + 3 = 291. Therefore, 0x123 in hexadecimal equals 291 in the decimal number system.
In the context of pointers in C, 0x123 could represent a memory address. All memory addresses are numbers, often represented as hexadecimal values for brevity and readability. However, it's important to note that 0x123 is likely too low to be a valid memory address in most modern systems. The specific range of valid memory addresses can vary widely depending on the system architecture and the operating system.
Here is a simple example of using 0x123 in a C program:
#include <stdio.h> |
In this code, we define an integer x and assign it the value 0x123. The compiler interprets 0x123 as a hexadecimal number and converts it to the equivalent decimal number (291). Then we print the value of x, which outputs 291.
The syntax for declaring a pointer in C is quite straightforward. You start with the type of data the pointer will point to, followed by an asterisk (*) and then the pointer's name. In essence, whatever is the datatype of the value that is held at the address, that will also be the datatype of a pointer to that address.
Here's an example:
int *p; |
In this case, 'p' is a pointer to an integer, and hence the data type for ‘p’ is an integer.
Pointers are a powerful feature of C, but they can be challenging to understand and use correctly. Here's a comprehensive guide on how to use pointers in C.
The first step in using pointers is to declare a pointer variable. This is done with the * operator. The syntax is as follows:
type *pointerName;
For instance, you can declare a pointer to an int like this:
int *p; |
This declares a variable p that can hold the address of an int.
After declaring a pointer, you can assign it the address of another variable using the & operator:
int x = 5; |
Here, p is a pointer to int, and we assign it the address of x. Now p points to x, which means it holds the memory address where x is stored.
Dereferencing a pointer means accessing the value present in the memory location pointed to by the pointer. This is done with the * operator:
int x = 5; |
When you have a pointer to a structure, you can access the members of the structure using the arrow (->) operator:
struct Person { |
In this example, p is a pointer to a Person structure. We use the arrow operator (->) to access the name and age members of the structure.
The arrow notation is a shorthand for dereferencing the pointer and then accessing a member. For instance, p->name equals (*p).name.
You can also pass pointers to functions. This allows the function to modify the original variables, not just copies:
void addOne(int *p) { |
In this example, the addOne function increments the value of the variable pointed to by p. We pass the address of x to the function.
Pointers in C can be of different types depending on the data type of the variable they are pointing to. They can also be categorised based on their characteristics and the level of indirection. Let's dive deeper into this topic.
A simple or basic pointer is a variable that holds the address of another variable. The pointer type should match the variable type it's pointing to.
int x = 10; |
In this example, p is a simple pointer that holds the address of the integer variable x.
An array pointer is a pointer that points to the first element of an array. You can increment the pointer to traverse the array.
int arr[5] = {1, 2, 3, 4, 5}; |
In this example, p is an array pointer that points to the first element of the array arr.
A pointer to a pointer is a form of multiple indirection. The first pointer contains the address of the second pointer, which points to the actual value.
int x = 5; |
A function pointer is a pointer that points to a function, allowing functions to be called indirectly.
void hello() { |
In this example, p is a function pointer that points to the hello function. We can call the hello function indirectly through p.
A null pointer is a pointer that doesn't point to any memory location. It's used to signify that the pointer is currently not pointing to a valid location.
int *p = NULL; |
In this example, p is a null pointer. The if statement checks if p is not null before trying to dereference it.
A void pointer, also known as a generic pointer, is a special type of pointer that can point to objects of any data type.
int x = 5; |
In this example, p is a void pointer that points to the integer x. To dereference the void pointer, we must cast it to the appropriate type (int * in this case).
The term "dangling pointer" refers to a pointer that doesn't point to an appropriate type of valid object. The pointer may still point to the deallocated memory's address if an object is removed or deallocated without changing the pointer's value.
int *p = (int *)malloc(sizeof(int)); |
In this example, p becomes a dangling pointer after we call free(p). The memory location it pointed to has been deallocated, but p still contains that address.
A wild pointer in C is a pointer that has not been initialised. It may point to some arbitrary memory location and can cause a program to crash or behave unpredictably.
int *p; // p is a wild pointer |
In this example, p is a wild pointer because it has not been initialised.
A constant pointer cannot change the address it's pointing to, and a pointer to a constant cannot change the value of the object it points to.
int x = 5, y = 10; |
In this example, p1 is a pointer to a constant, so we can't use p1 to change the value of x. On the other hand, p2 is a constant pointer, so we can't change the address stored in p2.
Pointers in C are extensively used and have numerous applications. Here are some of the most common use cases:
Dynamic memory allocation is one of the most important applications of pointers in C. With functions like malloc(), calloc(), realloc(), and free(), we can allocate memory dynamically on the heap during runtime.
Here's an example of dynamic memory allocation for an integer array:
int *arr = (int *)malloc(5 * sizeof(int)); // dynamically allocate an array of 5 integers |
Pointers are used to handle arrays, strings, and structures more efficiently. For example, strings in C are essentially arrays of characters, so you can use pointers to manipulate strings.
Here's an example of using a pointer to manipulate a string:
char *str = "Hello, World!"; |
In this example, the pointer p is used to traverse the string and print each character.
Just like the simple linked list above, more complex data structures like trees and graphs also leverage pointers to link their elements (nodes) together.
Pointers in C offer several significant advantages:
Pointers can be used to traverse arrays more efficiently than array indices. In C, the name of an array essentially points to the first element of the array. By incrementing the pointer, we can traverse the array without an explicit indexing variable. This method can be quicker as it involves fewer operations.
Example:
int arr[5] = {1, 2, 3, 4, 5}; |
In this example, *(p + i) is equivalent to arr[i], but we are using pointer arithmetic to access the elements instead of array indexing.
In languages without pointers, when you declare an array, you must specify the size at compile time, and it cannot be changed. However, in C, you can use pointers to allocate memory dynamically during runtime. This means you can request the exact amount of memory you need when your program is running, leading to more efficient usage of memory resources.
Pointers allow functions to modify the local variables of other functions. When a variable is passed to a function, the function gets a copy of the variable's value. Any changes it makes to the copy do not affect the original variable. However, by passing a pointer to the variable instead, the function can modify the original variable's value.
Example:
void addOne(int *p) { |
Despite their clear-cut benefits, Pointers do come with a set of drawbacks of disadvantages that you must take note of. These include:
We hope this tutorial has been helpful in your journey to understand Pointers in C. They are a fundamental concept in C and C++ that allow programmers direct access to memory. This allows for efficient code but also requires careful handling to avoid errors and vulnerabilities.
While tutorials play an exceptional role in strengthening concepts, courses like upGrad’s Executive Post Graduate Programme in Software Development powered by IIIT-B can greatly help your career flourish in the growing field of STEM.
1. How do I use pointers in C?
To use pointers in C, you need to understand two operators- 'address of' (&) and the 'dereference' (*) operator.
2. What are the different types of pointers in C?
There are several types of pointers in C, including null pointers, dangling pointers, wild pointers, and void pointers.
3. What are the advantages of using pointers in C?
Pointers allow for efficient array traversal, dynamic memory allocation, and indirect access to variables. They are also essential for implementing complex data structures.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.