Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Pointer declarations: int *p

An asterisk in a declaration specifies that the declared name has a pointer type. The declaration alone does not allocate a pointed-to object.

Version: C23

Syntax

int *p;
const int *read_only;
int * const fixed = &value;

int *p = &x; creates a pointer to int and initializes it with the address of the existing variable x. A local declaration consisting only of int *p; does not supply a valid address automatically. Establish a value before use; NULL from an appropriate header can be an initial value.

In int *p, n;, only p is a pointer; n is an ordinary int. Separate declarations improve readability. Writing int* p instead of int *p does not change this meaning.

const int *p restricts modification of the object through that pointer, but allows changing the address stored in p. int * const p = &x fixes the pointer value, not the pointed-to integer. These qualifiers do not repair an invalid object lifetime. Reading or writing through *p is described separately under dereferencing.

Examples

#include <stdio.h>

int main(void) {
    int value = 7;
    const int *p = &value;
    printf("%d\n", *p);
    return 0;
}

Result

7

Sources