Understanding C Pointers: Concepts, Operations, and Examples
1. What Is a Pointer and Why Is It Important?
Key Advantages of Using Pointers
- Efficient data access (indirect access)
- An alternative way to access variables (via memory address)
- Ability to manipulate low-level hardware (registers, memory-mapped I/O)
- Essential for building data structures (linked list, tree, graph)
- Efficient function parameter passing (pass by address)
2. Memory Address and Pointer Basics
Memory Address
Every byte in memory has an address, for example:
0x0000000012345678
This is simply an integer.
Pointer Variable Definition
A pointer is a variable used to store a memory address.
Example:
int a = 100;
printf("%p\n", &a); // Print the memory address of a
On a 64-bit System:
All pointer types have the same size:
8 bytes (64 bits)
Regardless of whether they point to int, char, or double.
3. Pointer Operators: & (address) and * (dereference)
✔️ The & Operator — Get Address
int a = 100;
int* p = &a;
Notes:
❌ You cannot take the address of an expression
❌ You cannot take the address of a constant
&(a + 1) // invalid
&10 // invalid
✔️ The * Operator — Dereference
Dereferencing means:
- Get the address stored in the pointer
- Interpret the memory using the pointer’s base type
- Access (read or write) the data
int a = 100;
int* p = &a;
printf("%d\n", *p); // prints 100
*p = 200; // writes 200 into memory of a
✔️ & and * Are Opposites
*p == a
&(*p) == p
*(&a) == a
4. Declaring and Initializing Pointers
Basic Syntax
base_type *pointer_name;
Examples:
int* p;
char* cp;
double* dp;
❗ Uninitialized Pointer = Wild Pointer (Dangerous)
int *p;
printf("%d\n", *p); // undefined behavior
✔ Correct Initialization
int *p = NULL;
int a = 10;
p = &a; // safe to dereference now
5. Basic Example: Pointer Operations
int a = 100;
printf("&a = %p\n", &a);
printf("sizeof(&a) = %lu\n", sizeof(&a)); // pointer size = 8 bytes
int* p = &a;
printf("*p = %d\n", *p);
*p = 200; // changes the value of a
6. Pointers and Arrays
The array name acts like the address of the first element:
int b[5] = {1, 2, 3, 4, 5};
int* p = b; // same as p = &b[0]
Accessing elements using pointers:
*(p+0) == b[0]
*(p+1) == b[1]
*(p+2) == b[2]
Because pointer arithmetic is based on element size:
p + 1 = p + sizeof(int)
7. Pointer Arithmetic
p + 1 does NOT mean “add 1”
It means:
Move the pointer forward by one element.
For int (4 bytes):
p + 1 = p + 4
Example:
int a = 100;
int *p = &a;
printf("%p\n", p);
printf("%p\n", p + 1);
8. Using Pointers to Traverse an Array
Method 1: Do not change the pointer
for(int i=0; i<5; i++)
{
printf("%d\n", *(p+i));
}
Method 2: Use p++ (changes pointer)
for(int i=0; i<5; i++)
{
printf("%d\n", *(p++));
}
After the loop, p points to:
b[5] — out-of-bounds
9. Pointer Subtraction
Pointer subtraction gives the number of elements between them.
Example:
int b[5] = {1,2,3,4,5};
int* p = b;
int* q = &b[2];
printf("q - p = %ld\n", q - p); // 2
10. Summary of Today’s Code
✔ Print address and dereference
int a = 100;
int* p = &a;
printf("%p %d\n", p, *p);
✔ Wild pointer example (incorrect)
int *p;
printf("%d\n", *p); // undefined behavior
✔ Pointer access to array
int b[5] = {1,2,3,4,5};
int *p = b;
for(int i=0;i<5;i++)
printf("%d %d\n", i, *(p+i));
✔ Pointer increment
for(int i=0;i<5;i++)
printf("%d %d\n", i, *(p++));
1240

被折叠的 条评论
为什么被折叠?



