Pointers point to specific areas in the memory. Pointers contain the address of a variable, which in turn may contain a value or even an address to another memory.
A pointer is a special variable that stores the memory address of another variable instead of storing the actual value. Pointers allow programs to access and manipulate data directly through memory addresses.
data_type *pointer_name;
Example:
int *ptr;
Here, ptr is a pointer variable that can store the address of an integer variable.
#include
int main() {
int num = 10;
int *ptr;
ptr = # // Store address of num
printf("Value of num = %d\n", num);
printf("Address of num = %p\n", &num);
printf("Value stored in ptr = %p\n", ptr);
printf("Value pointed to by ptr = %d\n", *ptr);
return 0;
}
| Operator | Name | Purpose |
|---|---|---|
& |
Address Operator | Returns the memory address of a variable. |
* |
Dereference Operator | Accesses the value stored at a memory address. |
#include
int main() {
int num = 25;
int *ptr = #
printf("%d", *ptr);
return 0;
}
The expression *ptr accesses the value stored at the address contained in
ptr, which is 25.
A pointer is a variable that stores the memory address of another variable. By using pointers, programmers can directly access memory, efficiently manage data, and build complex data structures and applications.