Pointers
What is a pointer?
A pointer is a variable that stores the memory address of another object, such as an integer variable.
Imagine every house has an address. The address tells you where the house is located. Similarly, a pointer stores the memory address where a variable is located.
Definition: A pointer is a special variable that stores the memory address of another variable.Declaring a pointer
Syntax:data_type *pointer_name;
Examples:
int *p;
float *fp;
char *cp;
int *p;declares p as a pointer to an integer.float *fp;declares fp as a pointer to a float.char *cp;declares cp as a pointer to a character.
The * in a declaration indicates a pointer type. It does not mean multiplication in this context.
Initializing a pointer
Pointer initialization means assigning a valid address to a pointer before using it to access an object.
#include
int main(void)
{
int x = 25;
int *p = &x;
printf("Value of x = %d\n", x);
printf("Address of x = %p\n", (void *)&x);
printf("Value of p = %p\n", (void *)p);
return 0;
}
Example output (the address varies between executions):
Value of x = 25
Address of x = 0x7ffd...
Value of p = 0x7ffd...
The address printed for x matches the value stored in p.
The indirection operator (*)
The indirection operator, also called the dereference operator, is used to access or modify the value stored at the memory address held by a pointer.
Suppose:
int x = 25;
int *p = &x;
Then:
| Expression | Meaning |
|---|---|
| x | Value of the variable x |
| &x | Address of x |
| p | Address stored in pointer p |
| *p | Value of the object pointed to by p |
Here, *p evaluates to 25. The pointer variable p stores &x, which is the address of the object x (with value 25). Dereferencing p using *p accesses the value stored in x.
Example: C Program demonstrating indirection
#include
int main(void)
{
int x = 25;
int *p = &x;
printf("x = %d\n", x);
printf("*p = %d\n", *p);
*p = 50;
printf("New value of x = %d\n", x);
return 0;
}
Output:
x = 25
*p = 25
New value of x = 50
Why did x change to 50?
The statement *p = 50; changes the object pointed to by p. Since p points to x, the value of x becomes 50.
Difference between & and *
| Operator | Name | Purpose | Example |
|---|---|---|---|
| & | Address-of | Obtains an object's address | p = &x; |
| * | Indirection / dereference | Accesses the object through a pointer | y = *p; |
Memory trick: & finds the address; * follows the address to access the object.
Remember that * has different meanings depending on its context:
int *p; // Declaration: p is a pointer
y = *p; // Expression: access the pointed-to object
Size of a pointer in C
The size of a pointer is the amount of memory required to store a memory address, generally it takes the size of int.
Size of pointers for different data types
#include
int main()
{
int *p1;
char *p2;
float *p3;
double *p4;
printf("Size of int pointer: %u bytes\n", sizeof(p1));
printf("Size of char pointer: %u bytes\n", sizeof(p2));
printf("Size of float pointer: %u bytes\n", sizeof(p3));
printf("Size of double pointer: %u bytes\n", sizeof(p4));
return 0;
}
Output:
Size of int pointer: 4 bytes
Size of char pointer: 4 bytes
Size of float pointer: 4 bytes
Size of double pointer: 4 bytes
Why is the pointer size the same for different data types?
A pointer stores the memory location of a variable, regardless of whether that variable is an int, char, float, or double.
Pointer to pointer or double pointer in C
Definition
A pointer to pointer, also called a double pointer, is a pointer that stores the address of another pointer. It is declared using two asterisks (**).
Syntax
data_type **pointer_name;
Example:
int **pp;
Here, pp is a double pointer that can store the address of an integer pointer.
Example: C Program using a double pointer
#include
int main()
{
int a = 10;
int *p = &a;
int **pp = &p;
printf("Value of a = %d\n", a);
printf("Value using p = %d\n", *p);
printf("Value using pp = %d\n", **pp);
return 0;
}
Output:
Value of a = 10
Value using p = 10
Value using pp = 10
Understanding p, *p, pp, *pp, and **pp
| Expression | Meaning | Result |
|---|---|---|
| a | Value of variable a | 10 |
| p | Address of a | Address of a |
| *p | Value stored at the address in p | 10 |
| pp | Address of pointer p | Address of p |
| *pp | Value of p (address of a) | Address of a |
| **pp | Value of a | 10 |
The actual address values depend on where the variables are stored in memory.