Pointers in C

A pointer stores the address of an object in memory. If int number = 10;, then an int * pointer can store the address of number and access or change that value indirectly.

Pointer storing the address of a C variable
Important: an uninitialized pointer does not automatically contain a null pointer. Initialize a pointer to a valid address or to NULL before using it.

Declare and Initialize a Pointer Top ↑

int number = 10;
int *ptr = &number;
  • &number gives the address of number.
  • ptr stores that address.
  • *ptr dereferences the pointer and accesses the value stored at that address.

Address-of and Dereference Operators Top ↑

#include <stdio.h>

int main(void) {
    int number = 105;
    int *ptr = &number;

    printf("number = %d\n", number);
    printf("*ptr   = %d\n", *ptr);
    printf("address of number = %p\n", (void *)&number);
    printf("ptr              = %p\n", (void *)ptr);

    return 0;
}

The exact address changes between systems and program runs. Use %p with a void * value when printing an address.

Change a Variable Through a Pointer Top ↑

#include <stdio.h>

int main(void) {
    int number = 5;
    int *ptr = &number;

    *ptr = 100;

    printf("number = %d\n", number);
    return 0;
}

Output

number = 100

Pointers Must Match the Pointed-to Type Top ↑

int number = 3;
int *int_ptr = &number;

double price = 12.5;
double *double_ptr = &price;

A pointer to int should point to an int object; a pointer to double should point to a double object. Incompatible pointer assignments normally require a diagnostic and should not be used to bypass the type system.

Add Two Values Through Pointers Top ↑

#include <stdio.h>

int main(void) {
    int first = 100;
    int second = 200;
    int *p1 = &first;
    int *p2 = &second;

    int sum = *p1 + *p2;

    printf("sum = %d\n", sum);
    return 0;
}

Swap Two Numbers Using Pointers Top ↑

#include <stdio.h>

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

int main(void) {
    int first = 5;
    int second = 10;

    swap(&first, &second);

    printf("first = %d, second = %d\n", first, second);
    return 0;
}

Output

first = 10, second = 5

Passing addresses lets the function modify the caller's variables. This is a common C technique for functions that need to update multiple values.

Pass a Pointer to a Function Top ↑

#include <stdio.h>

double increased_salary(const double *basic) {
    return *basic * 1.15;
}

int main(void) {
    double salary = 1000.0;

    printf("Final salary = %.2f\n", increased_salary(&salary));
    return 0;
}

A const pointer parameter is useful when a function only needs to read the pointed-to value.

Pointers and Arrays Top ↑

#include <stdio.h>

int main(void) {
    int values[] = {3, 4, 5, 6, 7};
    size_t count = sizeof values / sizeof values[0];
    int *ptr = values;

    for (size_t i = 0; i < count; i++) {
        printf("value = %d, address = %p\n",
               *ptr, (void *)ptr);
        ptr++;
    }

    return 0;
}

In most expressions, an array name such as values is converted to a pointer to its first element. Pointer arithmetic such as ptr++ advances by one array element, not necessarily by one byte.

Pointer Arithmetic Rules Top ↑

  • You may add or subtract an integer offset from a pointer when staying within the same array object (or one position past its end).
  • You may subtract two pointers into the same array to find their element distance.
  • Adding two pointers, multiplying pointers, or dividing pointers is not defined by C.
  • Comparisons and arithmetic on unrelated object pointers should not be used as if they belonged to one array.

Sum an Array Using a Pointer Top ↑

#include <stdio.h>

int sum_array(const int *values, size_t count) {
    int sum = 0;

    for (size_t i = 0; i < count; i++) {
        sum += values[i];
    }

    return sum;
}

int main(void) {
    int values[] = {1, 2, 3, 4, 5};
    size_t count = sizeof values / sizeof values[0];

    printf("Sum = %d\n", sum_array(values, count));
    return 0;
}

Character Pointer and a String Top ↑

#include <stdio.h>

int main(void) {
    const char text[] = "plus2net";
    const char *ptr = text;

    while (*ptr != '\0') {
        putchar(*ptr);
        ptr++;
    }

    putchar('\n');
    return 0;
}

Null Pointers Top ↑

int *ptr = NULL;

if (ptr != NULL) {
    printf("%d\n", *ptr);
}

Never dereference a null pointer. The check must happen before using *ptr.

Pointer Practice and Solutions Top ↑

Practice with addresses, arrays, searching, swapping, sorting and strings, then compare your work with the complete solution page.

Pointer practice solutions →





plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer