View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

All About Pointers in C

Updated on 09/05/20253,261 Views

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!

What Are Pointers in C?

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:

Syntax of Pointer in C

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

Rules for Declaring a Pointer in C

  • Always match the pointer type with the variable type.
  • Use * during declaration, but use & when assigning an address.
  • Initialize pointers before using them to avoid undefined behavior.
  • Be careful with pointer arithmetic — it must stay within valid memory bounds.

Declaration of Pointers in C

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. 

Initialization of Pointers in C

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.

Types of Pointers 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.

Null Pointer in C With Example and Output

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.

Void Pointer in C With Example and Output

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.

Wild Pointer in C With Example and Output

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.

Dangling Pointer in C With Example and Output

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

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.

Example of Pointer Arithmetic in C With Output

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

Example of Pointer With Array in C

#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 to Functions 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().

Example of Function Pointer in C

#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 to Structures in C

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.

Example of Pointer to Structure in C

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

Example Program Using Pointers in C

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

Conclusion

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!

FAQs on Pointers in C

1. What is the main purpose of using pointers in C?

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.

2. How do you declare a pointer in C?

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.

3. What is the meaning of NULL pointer in C?

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.

4. How are pointers and arrays related in C?

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.

5. What is pointer arithmetic in C?

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.

6. Can a pointer point to another pointer in C?

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.

7. What are dangling pointers in C, and how can you avoid them?

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.

8. How do you dynamically allocate memory using pointers in C?

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.

9. What is the difference between a pointer and a reference in C?

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.

10. How can you pass a pointer to a function in C?

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.

11. What is the use of the const keyword with pointers in C?

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.

12. Are function pointers possible in C, and why are they useful?

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.

13. What are wild pointers in C, and how can you prevent them?

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.

14. How can pointers help create linked lists in C?

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.

15. What are some best practices when working with pointers in C?

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.

image

Take a Free C Programming Quiz

Answer quick questions and assess your C programming knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Start Learning For Free

Explore Our Free Software Tutorials and Elevate your Career.

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

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.