C Pointers
1. What is a Pointer?
-
A pointer is a variable that stores the memory address of another variable.
-
Instead of holding a value directly, it holds where the value is stored.
Syntax:
-
data_type→ type of the variable the pointer points to -
*→ indicates the variable is a pointer
2. Declaring and Using Pointers
Output (example):
*ptris used to access the value stored at the address (dereferencing).
3. Changing Value Using Pointers
Output:
Using pointers, you can modify the variable directly via its address.
4. Pointer and Arrays
-
The name of an array is treated as a pointer to its first element.
Output:
5. Pointer to Pointer
-
A pointer can store the address of another pointer.
Output:
6. Pointers and Functions (Call by Reference)
Output:
Pointers allow functions to modify variables outside their scope.
7. Key Points About Pointers
-
Declaration:
data_type *ptr; -
Address-of operator
&: stores the memory address in pointer. -
Dereference
*: access or modify the value at the pointer. -
Arrays and pointers are closely related.
-
Pointers are essential for:
-
Dynamic memory allocation
-
Passing arguments by reference
-
Linked lists, trees, and other data structures
-
