C Program to use pointers

Category: C Program

Explore a concise explanation of a simple C program demonstrating the creation, initialization, and usage of pointers. Understand the ussage of pointers with different data types in C.

In the world of C programming, pointers play a crucial role in managing memory and accessing data directly in the memory. The following example program demonstrates the creation, initialization, and usage of pointers.

#include <stdio.h>

void main() {
    int number = 25;
    int *pointer; // Pointer declaration

    pointer = &number; // Storing the address of the variable 'number' in the pointer

    printf("Value of number: %d\n", number);
    printf("Address of number: %p\n", &number);
    printf("Value of pointer: %p\n", pointer); // Printing the address stored in the pointer
    printf("Value pointed to by pointer: %d\n", *pointer); // Dereferencing the pointer to get the value
}

Output

Value of number: 25
Address of number: 0x7fff209244f4
Value of pointer: 0x7fff209244f4
Value pointed to by pointer: 25
  • An integer variable number is created and initialized with the value 25.
  • A pointer pointer is declared to store the memory address of an integer.
  • The address of the variable number is assigned to the pointer using the address-of operator &.
  • The program then prints the value of number, the address of number, the value of the pointer, and the value pointed to by the pointer.

Recommended Posts