Pointer in C Programming: Basics and Examples

If you're diving into C programming and haven’t come across pointers yet, buckle up. Pointers are one of the most fascinating (and sometimes intimidating) concepts in C. But once you get the hang of them, they open up a world of possibilities. From dynamic memory allocation to building complex data structures, pointers are the secret sauce behind many C-powered applications.

Pointer in C Programming

In this guide, we’ll walk you through the basics of pointers, how to use them, their syntax, examples, and common pitfalls all explained in simple terms.

Want to master pointers and become confident in C?


Check out the C Programming Course at Uncodemy for hands-on learning.

What is a Pointer in C?

In simple words, a pointer is a variable that stores the address of another variable. Rather than storing a value directly (like int a = 5;), a pointer holds where that value is located in memory.

Why Use Pointers?

  • Efficient memory usage
  • Accessing arrays and strings
  • Dynamic memory allocation
  • For passing large structures to functions
  • Implementing data structures like linked lists, trees, etc.

Pointer Syntax in C

Copy Code

int *ptr;

This declares a pointer to an integer.

To assign the address of a variable to the pointer:

int a = 10;

int *ptr = &a;  // & gives the address of 'a'

To access the value stored at the pointer:

printf("%d", *ptr);  // prints 10

A Simple Pointer Example

Copy Code

#include <stdio.h>

int main() {

    int num = 42;

    int *ptr = &num;

    printf("Value of num: %d\n", num);

   printf("Address of num: %p\n", &num);

   printf("Pointer ptr stores address: %p\n", ptr);

    printf("Value at the address stored in ptr: %d\n", *ptr);

    return 0;

}

Output:

Value of num: 42

Address of num: 0x7ffde3c9e6bc

Pointer ptr stores address: 0x7ffde3c9e6bc

Value at the address stored in ptr: 42

Types of Pointers in C

1. Null Pointer

int *ptr = NULL;

Used to initialize a pointer when it doesn't point to any memory location.

2. Dangling Pointer

A pointer pointing to a memory location that has been deallocated or released.

3. Void Pointer

void *ptr;

Can point to any data type but must be typecast before dereferencing.

4. Wild Pointer

Uninitialized pointer. It can point anywhere often leads to crashes.

int *ptr; // wild pointer

Pointers and Arrays

Arrays and pointers in C are closely related.

Copy Code

int arr[] = {1, 2, 3};

int *ptr = arr;

Now ptr points to the first element of the array.

printf("%d\n", *(ptr + 1)); // Output: 2

Pointer to Pointer

Yes, a pointer can also point to another pointer.

Copy Code

int val = 5;

int *ptr = &val;

int **pptr = &ptr;

printf("%d\n", **pptr); // Output: 5

Function Pointers

Functions also have addresses. We can store them in pointers.

Copy Code

void greet() {

    printf("Hello from function pointer!\n");

}

int main() {

    void (*funcPtr)() = greet;

    funcPtr();

    return 0;

}

Pointers and Functions

Pass by Reference

Using pointers, we can pass addresses of variables to functions and modify them.

Copy Code

void update(int *x) {

    *x = *x + 10;

}

int main() {

    int num = 5;

    update(&num);

   printf("Updated value: %d\n", num); // Output: 15

    return 0;

}

Common Mistakes with Pointers

  • Not initializing pointers (wild pointers)
  • Dereferencing NULL or dangling pointers
  • Memory leaks when using dynamic memory
  • Forgetting to free allocated memory

Pointers and Dynamic Memory

You can allocate memory during runtime using malloc, calloc, realloc, and release it with free().

Copy Code

int *ptr = malloc(sizeof(int));

*ptr = 25;

printf("%d", *ptr);

free(ptr);

FAQs on Pointers in C

Q1. Why are pointers important in C?
Because they allow low-level memory access and give programmers control over memory operations.

Q2. Can a pointer point to another pointer?
Yes. This is called a pointer to a pointer.

Q3. What is a NULL pointer?
A pointer that doesn’t point to any valid memory address.

Q4. What is pointer arithmetic?
It involves moving the pointer around using operations like increment, decrement, etc.

Q5. Are pointers used in other languages?
Not directly. Most high-level languages abstract away pointers, but they are heavily used in C and C++.

Conclusion

Pointers in C might take some getting used to, but once you do, you’ll wonder how you ever lived without them. They give you the power to manipulate memory directly, making your programs more efficient and flexible.

Whether you’re working with arrays, functions, or dynamic memory, mastering pointers will set a strong foundation for systems-level programming.

Looking for real-world projects and in-depth explanations of pointers?
Start your C programming journey with Uncodemy’s expert-led tutorials today!

Placed Students

Our Clients

Partners

...

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses