C Programming Tutorial: Pointers

Welcome to C Programming Tutorial

Your guide to getting started with C programming.

Pointer

1. Basics of Pointers

Understanding Memory Addresses and Pointer Variables

A pointer is a variable that stores the memory address of another variable. Instead of holding a data value directly, a pointer holds the location of where the value is stored in memory.

#include <stdio.h>

int main() {
    int num = 10;        // Regular integer variable
    int *ptr = #     // Pointer variable holding the address of 'num'

    printf("Value of num: %d\n", num);      // Output: 10
    printf("Address of num: %p\n", &num);   // Output: Address of num (e.g., 0x7ffeefbff5c4)
    printf("Value of ptr: %p\n", ptr);      // Output: Address of num (same as above)
    printf("Value pointed to by ptr: %d\n", *ptr); // Output: 10

    return 0;
}
            

Explanation:

int *ptr = # initializes the pointer ptr with the address of the variable num.

*ptr is used to dereference the pointer and access the value at the memory address stored in ptr.

2. Pointer Arithmetic

Pointers can be incremented or decremented, and you can perform arithmetic operations like addition or subtraction on them. This is useful for iterating through arrays.

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *ptr = arr;  // Points to the first element of the array

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

    ptr++;  // Increment pointer to point to the next element
    printf("After incrementing, second element: %d\n", *ptr); // Output: 20

    return 0;
}
            

Explanation:

int *ptr = arr; initializes the pointer ptr to point to the first element of the array arr.

*(ptr + 1) accesses the second element of the array. Pointer arithmetic is performed based on the size of the type it points to (int in this case).

3. Pointer to Functions and Arrays

Pointer to Functions

A pointer to a function allows you to call a function indirectly through the pointer. This is often used for callback functions or dynamic function dispatching.

#include <stdio.h>

// Function to be pointed to
void greet() {
    printf("Hello, World!\\n");
}

int main() {
    void (*funcPtr)() = greet; // Function pointer initialization

    funcPtr(); // Calling the function using the pointer

    return 0;
}
            

Explanation:

void (*funcPtr)() declares a function pointer funcPtr that points to a function returning void and taking no arguments.

funcPtr = greet; assigns the address of the function greet to the function pointer.

funcPtr(); calls the function via the pointer.

Pointer to Arrays

You can use pointers to iterate through elements of an array. The name of an array itself is a pointer to its first element.

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int *ptr = arr;  // Pointer to the first element of the array

    // Accessing array elements using pointer
    for (int i = 0; i < 5; i++) {
        printf("Element %d: %d\\n", i, *(ptr + i));
    }

    return 0;
}
            

Explanation:

int *ptr = arr; initializes the pointer ptr to point to the first element of the array arr.

*(ptr + i) accesses the i-th element of the array using pointer arithmetic.

Previous Next
Modern Footer