DEV Community

Dolly Sharma
Dolly Sharma

Posted on

Pointer in C++

Understanding Pointer Size and Dereferencing in C/C++

Introduction

Pointers are an important concept in C and C++. A pointer is a variable that stores the memory address of another variable. Instead of storing a value directly, it points to the location where the value is stored in memory. Understanding pointer size, dereferencing, and pointer assignment is essential for efficient memory management.


Pointer Size in C/C++

The size of a pointer does not depend on the data type it points to. Instead, it depends on the system architecture (32-bit or 64-bit).

For example, the following pointer types:

  • int*
  • char*
  • float*
  • double*
  • long long*
  • void*

all have the same size on the same system.

Pointer Size on Different Systems

System Type Pointer Size
32-bit system 4 bytes
64-bit system 8 bytes

Most modern computers use 64-bit architecture, so the pointer size is usually 8 bytes.


Example Program

#include <stdio.h>

int main() {
    printf("%zu\n", sizeof(int*));
    printf("%zu\n", sizeof(char*));
    printf("%zu\n", sizeof(double*));
    printf("%zu\n", sizeof(void*));
}
Enter fullscreen mode Exit fullscreen mode

Output on a 64-bit system:

8
8
8
8
Enter fullscreen mode Exit fullscreen mode

This shows that all pointer types occupy the same memory size.


Size of Some Common Data Types

Data Type Size
char 1 byte
int 4 bytes
float 4 bytes
double 8 bytes
long long 8 bytes
pointer (int*, char*, etc.) 8 bytes (in 64-bit system)

Although double, long long, and pointers may all have 8 bytes, they store different types of data.

  • double → stores floating point numbers
  • long long → stores large integers
  • pointer → stores memory addresses

Changing the Address Stored in a Pointer

A pointer can be reassigned to point to another variable.

Example:

int a = 10;
int b = 20;

int *p;

p = &a;   // pointer points to a
p = &b;   // pointer now points to b
Enter fullscreen mode Exit fullscreen mode

Here the pointer changes the address it stores.


Null Pointer

A pointer can also be assigned a NULL value, which means it does not point to any valid memory location.

Example:

int *p = NULL;
Enter fullscreen mode Exit fullscreen mode

However, dereferencing a NULL pointer is invalid and may cause a program crash.


Dereferencing a Pointer

Dereferencing means accessing the value stored at the memory address held by the pointer.
The dereference operator is *.

Example:

int a = 10;
int *p = &a;

printf("%d", *p);
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • p stores the address of a
  • *p accesses the value stored at that address

Output:

10
Enter fullscreen mode Exit fullscreen mode

Accessing Pointer Address and Value

Expression Meaning
p address stored in pointer
*p value at that address
&a address of variable a

Example:

printf("%p\n", p);   // prints address
printf("%d\n", *p);  // prints value
Enter fullscreen mode Exit fullscreen mode

Modifying Value Using Pointer

A pointer can also be used to modify the value of the variable it points to.

Example:

int a = 10;
int *p = &a;

*p = 50;
printf("%d", a);
Enter fullscreen mode Exit fullscreen mode

Output:

50
Enter fullscreen mode Exit fullscreen mode

Here *p directly modifies the value stored in the memory location of a.


Conclusion

Pointers are powerful features in C and C++. The size of a pointer depends on the system architecture rather than the data type. On most modern systems, pointer size is 8 bytes. Pointers allow programmers to access memory addresses, change variable values through dereferencing, and efficiently manage memory.

Understanding pointers is fundamental for working with arrays, dynamic memory allocation, data structures, and system-level programming.

Top comments (0)