Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. Pointer Basics (* / &)

Pointer Basics (* / &)

A pointer is a variable that stores the memory address of another variable. Use the & operator to get an address, and the * operator to read or write the value at that address. Pointers are a core feature of C and are fundamental to arrays, strings, dynamic memory, and function pointers.

Syntax

// Declares a pointer variable (a pointer to the specified type).
type *pointerName;

// Gets the address of a variable (address-of operator).
&variableName

// Reads or writes the value at the address the pointer points to (dereference operator).
*pointerName

// Declares and initializes a pointer at the same time.
type *pointerName = &variableName;

// Null pointer (represents a pointer that points to nothing).
type *pointerName = NULL;

Pointer Operators

OperatorNameDescription
&Address-of operatorReturns the memory address of a variable. Used to assign to a pointer or pass to a function.
* (in declaration)Pointer declarationIndicates that a variable is a pointer. Written after the type name.
* (in expression)Dereference operatorReads or writes the value at the address the pointer points to. Also called dereferencing.
NULLNull pointer constantRepresents a pointer that does not point to any address. Used to initialize pointers or indicate an invalid state.

Sample Code

#include <stdio.h>

// Modifies the caller's variable through a pointer.
void double_value(int *p) {
    *p = *p * 2; // Doubles the value at the address the pointer points to.
}

// Swaps the values of two variables.
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main(void) {
    int x = 10;
    int *p = &x; // Stores the address of x in p.

    printf("Value of x: %d\n", x);              // Prints '10'.
    printf("Address of x: %p\n", (void *)&x);
    printf("Value of p (address): %p\n", (void *)p);
    printf("Value p points to: %d\n", *p);      // Prints '10'.

    // Modifies the value through the pointer.
    *p = 20;
    printf("x after modification: %d\n", x);    // Prints '20'.

    // Passes a pointer to a function to modify the value.
    double_value(&x);
    printf("x after doubling: %d\n", x);        // Prints '40'.

    // Swapping values.
    int a = 5, b = 9;
    swap(&a, &b);
    printf("a = %d, b = %d\n", a, b);           // Prints 'a = 9, b = 5'.

    // Example of a NULL check.
    int *q = NULL;
    if (q == NULL) {
        printf("The pointer is NULL.\n");
    }

    return 0;
}

Notes

In C, function arguments are passed by value (copied) by default. To modify a variable inside a function, pass a pointer to it. The function can then write to that address to change the caller's variable.

Dereferencing an uninitialized pointer or a pointer to freed memory causes crashes and undefined behavior. Always initialize pointers when you declare them, and assign NULL to them after you are done using them.

For the relationship between pointers and arrays, see Pointers and Arrays. For dynamic memory allocation and deallocation, see malloc() / free().

If you find any errors or copyright issues, please .