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
When you debug C programs, you often encounter memory-related bugs that can confuse even experienced developers. This is where pointers in C come into the picture. They play a vital role in giving you fine control over memory, which helps you write efficient and flexible programs.
As you work with pointers in C, you can solve tasks like dynamic memory allocation, passing large data structures to functions, or building linked data structures. Without pointers, many real-world programs would either break or become inefficient.
Pursue our Software Engineering courses to get hands-on experience!
Pointers in C are variables that store the memory address of another variable. Instead of holding actual data, a pointer contains the address where the data is located. This powerful feature allows for efficient memory management, direct manipulation of data, and the creation of complex data structures like linked lists. By working with pointers, you can pass large data structures between functions without copying them, improving performance.
Boost your skills by enrolling in these top-rated programs:
The syntax of a pointer in C is straightforward. You declare a pointer by placing an asterisk * before the pointer name. For example:
int *ptr;
Here, ptr is a pointer to an integer.
You can also assign the address of a variable to a pointer:
int num = 10;
int *ptr = #
Declaring a pointer reserves memory to store an address. The general form is:
type *pointer_name;
Example:
int *p;
float *q;
char *name;
In this example, p points to an int, q to a float, and name to a char.
A pointer should always be initialized before use. You can initialize it using the address-of operator &:
int x = 20;
int *p = &x;
Output:
Value of x = 20
Address stored in p = (some memory address)
Explanation: Here, p holds the address of x, and dereferencing *p will give the value 20.
As you delve deeper into pointers in C, you may encounter more advanced topics such as pointer to pointer, which expands on pointer handling in Pointer to Pointer in C.
In C, pointers are categorized into different types based on their usage and behavior. Each type serves a specific purpose and offers unique capabilities for memory management. Void pointers are versatile, capable of holding addresses of any data type. Null pointers are used to indicate that a pointer doesn't point to a valid memory location. Wild pointers are uninitialized and can cause undefined behavior, while dangling pointers point to memory that has been deallocated. Let’s discuss them in detail.
A null pointer points to nothing:
int *p = NULL;
Example:
#include <stdio.h>
int main() {
int *p = NULL;
printf("p = %p\n", p);
return 0;
}
Output:
p = (nil) or 0x0 (platform dependent)
Explanation: p is initialized to NULL, which means it doesn't point to any valid memory.
A void pointer can point to any data type:
#include <stdio.h>
int main() {
int x = 10;
void *p = &x;
printf("Address stored in p = %p\n", p);
return 0;
}
Output:
Address stored in p = 0x61fe14 (sample address)
Explanation: p holds the address of x. To use its value, you must cast it to the correct type.
A wild pointer is uninitialized and can cause undefined behavior:
#include <stdio.h>
int main() {
int *p;
printf("Value of p = %p\n", p);
return 0;
}
Output:
Value of p = 0x61ff10 (random address)
Explanation: p is uninitialized and holds a garbage address, which can crash the program if dereferenced.
A dangling pointer points to a memory location that has been freed:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = (int *)malloc(sizeof(int));
*p = 25;
free(p);
printf("Dangling pointer p = %p\n", p);
return 0;
}
Output:
Dangling pointer p = 0x5648e0
Explanation: After freeing, p still holds the address, but accessing it is unsafe.
Pointer arithmetic allows moving through memory blocks:
#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d\n", *(p + 1));
return 0;
}
Output:
20
Explanation: *(p + 1) accesses the second element of the array. Pointer arithmetic works based on the type size.
#include <stdio.h>
int main() {
int arr[] = {5, 15, 25};
int *p = arr;
p++;
printf("%d\n", *p);
return 0;
}
Output:
15
Explanation: p++ moves the pointer to the next integer in the array, which is 15.
Checkout our guide to Dynamic Memory Allocation in C.
Pointers and arrays are closely related:
#include <stdio.h>
int main() {
int arr[] = {100, 200, 300};
int *p = arr;
printf("%d\n", *(p + 2));
return 0;
}
Output:
300
Explanation: *(p + 2) gives the third element of the array.
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3};
int *p = arr;
for(int i = 0; i < 3; i++) {
printf("%d ", *(p + i));
}
return 0;
}
Output:
1 2 3
Explanation: The loop prints array elements using pointer arithmetic.
A common application of pointers in C is the array of pointers, which you can explore further in our article on Array of Pointers in C.
Pointers in C are not limited to regular variables but can also be used to reference functions, as explained in our article on Function Pointers in C. A pointer can store the address of a function:
#include <stdio.h>
void greet() {
printf("Hello, Ram!\n");
}
int main() {
void (*funcPtr)() = greet;
funcPtr();
return 0;
}
Output:
Hello, Ram!
Explanation: funcPtr holds the address of greet and calls it using funcPtr().
#include <stdio.h>
void welcome(char name[]) {
printf("Welcome, %s!\n", name);
}
int main() {
void (*funcPtr)(char[]) = welcome;
funcPtr("Ankita");
return 0;
}
Output:
Welcome, Ankita!
Explanation: The function pointer calls welcome with "Ankita" as the argument.
Pointers can also point to structures:
#include <stdio.h>
struct Person {
char name[20];
int age;
};
int main() {
struct Person p1 = {"Pooja", 22};
struct Person *ptr = &p1;
printf("%s is %d years old\n", ptr->name, ptr->age);
return 0;
}
Output:
Pooja is 22 years old
Explanation: The -> operator accesses structure members using a pointer.
#include <stdio.h>
struct Student {
char name[20];
int marks;
};
int main() {
struct Student s = {"Prasun", 85};
struct Student *ptr = &s;
printf("%s scored %d marks\n", ptr->name, ptr->marks);
return 0;
}
Output:
Prasun scored 85 marks
Explanation: ptr holds the address of s, and ptr->marks gives the student’s marks.
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
Output:
x = 20, y = 10
Explanation: The swap function uses pointers to swap the values of x and y.
Using pointers allows us to pass arguments by reference in C, enhancing efficiency. Explore how this works in our article on Call by Reference in C.
Understanding pointers in C is essential for writing efficient programs. With them, you can manipulate memory directly, improve performance, and build complex data structures. Always handle pointers carefully, initialize them, and avoid common pitfalls like dangling or wild pointers. Mastering pointers gives you the power to write robust and optimized C code.
So, keep experimenting, practicing, and exploring the world of pointers in C. With each program you write, you’ll gain more confidence and sharpen your skills. Don’t be afraid of mistakes—they are the best teachers in your programming journey. Stay curious, stay persistent, and soon you’ll master pointers and unlock the true potential of C programming!
Pointers in C help directly access and manipulate memory, improve program efficiency, support dynamic memory allocation, and allow functions to modify variables outside their local scope.
You declare a pointer by using the * symbol with a data type, like int *ptr;. This tells the compiler that ptr will store the address of an integer variable.
A NULL pointer is a pointer that does not point to any valid memory location. It is commonly used to check whether a pointer is initialized or not before dereferencing.
Pointers and arrays in C are closely related because the array name acts like a pointer to its first element. You can use pointers to traverse or modify array elements efficiently.
Pointer arithmetic allows you to perform operations like increment, decrement, addition, or subtraction on pointers. This is especially useful when working with arrays or dynamic memory.
Yes, in C, you can have a pointer to another pointer, called a double pointer (**ptr). Double pointers are often used for dynamic memory allocation of multidimensional arrays.
Dangling pointers refer to pointers that point to freed or invalid memory. To avoid them, set pointers to NULL after freeing memory to prevent accidental access.
You use functions like malloc(), calloc(), or realloc() from <stdlib.h> to allocate memory dynamically. Always check if the allocation was successful and free the memory afterward.
C does not have references like C++. A pointer holds a memory address, while a reference (in languages like C++) is an alias for another variable. C relies only on pointers.
You pass a pointer by providing the variable’s address to the function. This allows the function to modify the original variable’s value or access large data without copying it.
Using const with pointers prevents modifying the pointed-to data or the pointer itself. It helps improve code safety and readability by making clear what should remain unchanged.
Yes, function pointers are possible in C. They store the address of a function and allow calling functions dynamically, enabling callback mechanisms and flexible program design.
Wild pointers are uninitialized pointers that point to unpredictable memory. Always initialize pointers either to NULL or a valid memory address to avoid unexpected behavior.
Pointers are essential in linked lists because each node uses a pointer to connect to the next node. This dynamic connection allows flexible insertion, deletion, and traversal of data.
Always initialize pointers, check for NULL before dereferencing, free allocated memory, and avoid using freed or uninitialized pointers to prevent memory leaks and undefined behavior.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author|900 articles published
Previous
Next
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
+918068792934
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.