C C++

Declaring Pointers in C


Introduction

A pointer declaration creates a pointer variable that can store the memory address of another object. Before a pointer can be used, it must be declared with the correct type.

Pointers are one of the most powerful features of the C programming language. They enable efficient memory access, dynamic memory allocation, function parameter passing, and the implementation of complex data structures such as linked lists and trees.

Understanding how to declare pointers correctly is the first step toward mastering pointer programming in C.

What Is a Pointer Declaration?

A pointer declaration tells the compiler that a variable will store the address of an object of a specific type.

Syntax

data_type *pointer_name;

The declaration consists of:

  • The type of object the pointer will reference.
  • The * declarator indicating a pointer.
  • The pointer variable name.

For example:

Example

int *ptr;

This declaration creates a pointer named ptr that can store the address of an int.

Pointer Declaration vs Pointer Initialization

Declaring a pointer does not automatically assign it a valid memory address.

Declaration Initialization
Creates the pointer variable. Assigns a value to the pointer.
int *ptr; ptr = &value;

A newly declared local pointer contains an indeterminate value until it is initialized.

Warning: Never dereference an uninitialized pointer. It does not point to a valid object and using it results in undefined behavior.

Declaring Different Types of Pointers

The pointer's base type determines the type of object it can reference.

Declaration Description
char *ptr; Pointer to a character.
int *ptr; Pointer to an integer.
float *ptr; Pointer to a float.
double *ptr; Pointer to a double.
struct Node *ptr; Pointer to a structure.

Declaring and Initializing a Pointer

A pointer is commonly initialized when it is declared.

Example

#include <stdio.h>

int main(void)
{
    int number = 100;

    int *ptr = &number;

    printf("%d\n", *ptr);

    return 0;
}

Output

100

The pointer stores the address of number, and the dereference operator accesses the value stored at that address.

Declaring a NULL Pointer

If a pointer does not yet reference an object, initialize it with NULL.

Example

#include <stddef.h>

int *ptr = NULL;
Note: Initializing a pointer with NULL is much safer than leaving it uninitialized because it clearly indicates that the pointer does not currently reference an object.

Declaring Multiple Pointers

When declaring multiple variables, the * belongs to each variable, not to the data type.

Correct Declaration

Example

int *ptr1;
int *ptr2;

Or:

Example

        
int *ptr1, *ptr2;
        
    

Common Mistake

Example

int *ptr1, ptr2;

In this declaration:

  • ptr1 is a pointer.
  • ptr2 is a regular integer.
Warning: The * is part of the declarator, not the data type. Always verify each variable in a multiple declaration.

Pointer Declaration Style

There are several common formatting styles for pointer declarations.

Example

int* ptr;

int *ptr;

int * ptr;

All three declarations are equivalent and accepted by the compiler.

Tip: Many C programmers prefer writing int *ptr; because it clearly shows that the asterisk belongs to the variable declaration.

Declaring a Pointer to a Pointer

A pointer can store the address of another pointer.

Syntax

data_type **pointer_name;

Example

int value = 10;

int *ptr = &value;

int **pptr = &ptr;

Here:

  • ptr points to value.
  • pptr points to ptr.

Declaring Constant Pointers

The const keyword affects whether the pointer or the pointed-to data can be modified.

Declaration Meaning
const int *ptr; Pointer to constant data.
int *const ptr = &value; Constant pointer.
const int *const ptr = &value; Constant pointer to constant data.

These declarations are discussed in more detail in the topic on const pointers.

Common Beginner Mistakes

  • Using a pointer before initializing it.
  • Confusing a pointer declaration with an ordinary variable declaration.
  • Assuming all variables in a multiple declaration are pointers.
  • Dereferencing a NULL or uninitialized pointer.
  • Using the wrong pointer type for an object.

Best Practices

  • Initialize pointers when declaring them.
  • Use NULL if no valid object is available.
  • Declare one pointer per line to improve readability.
  • Use meaningful pointer names.
  • Match the pointer type with the type of the object it references.
Tip: Declaring one pointer per statement makes your code easier to read and avoids mistakes in multiple declarations.

Summary

Declaring pointers correctly is fundamental to working with memory in C. A pointer declaration specifies the type of object the pointer can reference and creates a variable capable of storing an address.

Declaration Description
int *ptr; Pointer to an integer.
char *ptr; Pointer to a character.
double *ptr; Pointer to a double.
int **ptr; Pointer to a pointer.
int *ptr = NULL; Safely initialized pointer.

Once you understand how pointers are declared, you can confidently move on to learning pointer arithmetic, arrays and pointers, function pointers, dynamic memory allocation, and other advanced pointer-related topics in C.