• Home
  • Pointer Basics – Pointer is an Integer

Pointer Basics – Pointer is an Integer

View Categories

Pointer Basics – Pointer is an Integer

< 1 min read

This example shows that a pointer stores a memory address, which is an integer value. While pointers hold addresses, their size depends on the system architecture and may differ from standard integer types.



⚙️
main.c

Copy to clipboard

#include <stdio.h>

int main()
{
    int num;
    int *ptr;

    num = 5;
    ptr = (int *)5;

    printf("num = %d\n", num);
    printf("ptr = %p\n", ptr);

    printf("sizeof int = %zu\n", sizeof(int));
    printf("sizeof long = %zu\n", sizeof(long));
    printf("sizeof pointer = %zu\n", sizeof(int *));

    return 0;
}

Output:
num = 5
ptr = 0x5
sizeof int = 4
sizeof long = 8
sizeof pointer = 8

A pointer stores a memory address, which is represented as an integer value internally.

Assigning num = 5 stores the value 5 in an integer variable.
Assigning ptr = (int *)5 stores an address value in the pointer. This is generally not valid in practice, but is used here to illustrate that pointers hold integer-type values.

Addresses are always whole numbers; they are never fractional.

The size of a pointer depends on the system architecture. On a 32-bit system it is typically 4 bytes, and on a 64-bit system it is typically 8 bytes.

This is why a pointer may not always fit into a standard int type.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *