C Pointers


In C programming, a pointer is a variable that stores the memory address of another variable. Pointers provide a way to directly access and manipulate memory, making C a powerful language for low-level programming, dynamic memory management, and efficient data handling.

Pointers are essential for tasks like passing large data to functions efficiently, dynamic memory allocation, building data structures (like linked lists), and interacting with arrays. Understanding pointers is crucial for writing advanced C programs that are both fast and memory-efficient.

Declaring Pointers

A pointer is declared by specifying the type of variable it will point to, followed by an asterisk (*) and the pointer name:

data_type *pointer_name;
  • data_type: The type of variable the pointer will point to (int, float, char, etc.).

  • pointer_name: The name of the pointer variable.

Example:

int *ptr; // pointer to an integer
float *fptr; // pointer to a float
char *cptr; // pointer to a char

Pointers must always match the type of variable they are pointing to.

Assigning Addresses to Pointers

Once declared, a pointer can store the address of a variable using the address-of operator (&):

#include <stdio.h>

int main() {
    int num = 10;
    int *ptr;

    ptr = &num; // store address of num in ptr
    printf("Value of num: %d\n", num);
    printf("Address of num: %p\n", &num);
    printf("Value stored in ptr (address of num): %p\n", ptr);

    return 0;
}
  • ptr now points to num.

  • %p is used to print memory addresses in a readable format.

Dereferencing Pointers

Dereferencing a pointer means accessing or modifying the value stored at the memory address the pointer holds. This is done using the * operator:

#include <stdio.h>

int main() {
    int num = 50;
    int *ptr = &num;

    printf("Value of num using pointer: %d\n", *ptr);

    *ptr = 100; // modify value through pointer
    printf("New value of num: %d\n", num);

    return 0;
}
  • *ptr gives access to the value stored at the address held by the pointer.

  • Modifying *ptr directly affects the original variable.

Pointer and Arrays

Pointers and arrays are closely related in C. The name of an array acts as a pointer to its first element:

#include <stdio.h>

int main() {
    int arr[3] = {10, 20, 30};
    int *ptr = arr; // equivalent to &arr[0]

    printf("First element: %d\n", *ptr);
    printf("Second element: %d\n", *(ptr + 1));
    printf("Third element: %d\n", *(ptr + 2));

    return 0;
}
  • Pointer arithmetic allows you to traverse arrays efficiently.

  • Adding 1 to a pointer moves it to the next element of the same data type.

Pointers and Functions

Pointers allow you to pass variables by reference to functions, enabling the function to modify the original variable:

#include <stdio.h>

void increment(int *p) {
    *p = *p + 1;
}

int main() {
    int num = 5;
    increment(&num);
    printf("Incremented value: %d\n", num);
    return 0;
}
  • Passing &num allows the function to work directly with the variable’s memory.

  • This is more efficient than copying large data structures.

Pointers to Pointers

C allows pointers to pointers, which are variables that store the address of another pointer:

#include <stdio.h>

int main() {
    int num = 10;
    int *ptr = &num;
    int **pptr = &ptr;

    printf("Value of num using pointer to pointer: %d\n", **pptr);
    return 0;
}
  • pptr points to ptr, which points to num.

  • Dereferencing twice (**pptr) accesses the original value.

Null Pointers

A null pointer is a pointer that does not point to any valid memory. It is initialized with NULL:

#include <stdio.h>

int *ptr = NULL;
  • Null pointers are used to prevent accidental access to uninitialized memory.

  • Always check if a pointer is NULL before dereferencing.

Pointer Arithmetic

Pointers can be incremented, decremented, or compared:

#include <stdio.h>

int main() {
    int arr[3] = {1, 2, 3};
    int *ptr = arr;

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

    return 0;
}
  • Incrementing a pointer moves it to the next memory location of its type.

  • Decrementing moves it back.

Common Mistakes with Pointers

  1. Dereferencing uninitialized or null pointers.

  2. Using pointers after freeing memory (dangling pointers).

  3. Confusing pointer types (int* vs float*).

  4. Forgetting to use & when storing a variable’s address.

  5. Incorrect pointer arithmetic leading to undefined behavior.

Practical Example: Swapping Two Numbers Using Pointers

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 10, y = 20;
    printf("Before swap: x = %d, y = %d\n", x, y);
    swap(&x, &y);
    printf("After swap: x = %d, y = %d\n", x, y);
    return 0;
}
  • Demonstrates pass-by-reference using pointers.

  • Allows the function to modify the original variables directly.

Summary of the Tutorial

Pointers in C are variables that store memory addresses. They allow programs to access and modify data directly, work efficiently with arrays and functions, and handle dynamic memory. Mastery of pointers is essential for building advanced, efficient, and flexible C programs, including linked lists, dynamic arrays, and memory management tasks.


Practice Questions

  1. Write a program to store an integer in a variable and print its value and memory address using a pointer.

  2. Create a program to swap two numbers using pointers.

  3. Write a program to increment the value of a variable using a pointer.

  4. Develop a program to print all elements of an array using a pointer.

  5. Write a program to pass a pointer to a function and modify the value of the original variable.

  6. Create a program to find the sum of array elements using a pointer.

  7. Write a program to declare a pointer to a pointer and access the original variable’s value.

  8. Develop a program to compare two integers using pointers and print the larger value.

  9. Write a program to reverse an array using pointers.

  10. Create a program to print each character of a string using a pointer.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top