How to declaring pointer variables in C/C++?

A pointer is a variable that stores the memory address of another variable. To declare pointer variables in C, we use the asterisk (*) operator before the variable name during declaration.

Syntax

data_type *pointer_name;

Where data_type is the type of variable the pointer will point to, and pointer_name is the name of the pointer variable.

Basic Pointer Declaration and Usage

Here's how to declare and use pointer variables in C −

#include <stdio.h>

int main() {
    // A normal integer variable
    int a = 7;
    
    // A pointer variable that holds address of a
    int *p = &a;
    
    // Value stored is value of variable "a"
    printf("Value of Variable : %d\n", *p);
    
    // It will print the address of the variable "a"
    printf("Address of Variable : %p\n", p);
    
    // Reassign the value through pointer
    *p = 6;
    printf("Value of the variable is now: %d\n", *p);
    
    return 0;
}
Value of Variable : 7
Address of Variable : 0x6ffe34
Value of the variable is now: 6

Multiple Pointer Declarations

You can declare multiple pointers of the same type in a single statement −

#include <stdio.h>

int main() {
    int x = 10, y = 20;
    
    // Declare multiple pointers
    int *ptr1, *ptr2;
    
    // Assign addresses
    ptr1 = &x;
    ptr2 = &y;
    
    printf("Value at ptr1: %d\n", *ptr1);
    printf("Value at ptr2: %d\n", *ptr2);
    
    return 0;
}
Value at ptr1: 10
Value at ptr2: 20

Key Points

  • The * operator is used for declaration and dereferencing pointers.
  • The & operator gets the address of a variable.
  • Pointers must be declared with the same data type as the variable they point to.
  • Uninitialized pointers can cause runtime errors − always initialize them.

Conclusion

Pointer declaration in C uses the asterisk (*) syntax. Pointers store memory addresses and provide indirect access to variables, making them essential for dynamic memory allocation and efficient programming.

Updated on: 2026-03-15T10:45:05+05:30

545 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements