Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

*pointer: accessing an object through a pointer

The unary * operator refers to the object or function designated by a pointer. It does not create an object or allocate memory.

Version: C23

Syntax

*pointer

If p points to an existing variable x, the expression *p designates that variable. You can read its value and, if the object is modifiable, assign a new one. &x obtains an address; *p follows an address. The pointer type determines the pointed-to object type.

Before using it, establish that the pointer is valid, the object is alive, and the access is within its bounds. Do not dereference NULL, an indeterminate pointer, a pointer to freed memory, or a pointer past the end of an array. Merely checking that a pointer is not NULL does not establish the other conditions.

In the example, p points to the local variable x throughout its lifetime. Assigning *p = 9 therefore also changes the value observed through x. The asterisk in a * b performs multiplication, and in int *p it contributes to a declaration; these meanings are distinct.

Examples

#include <stdio.h>

int main(void) {
    int x = 4;
    int *p = &x;
    *p = 9;
    printf("%d %d\n", x, *p);
    return 0;
}

Result

9 9

Sources