Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧠 Introduction
One of the defining features of C—and a major reason for its
power—is its use of pointers. Pointers allow direct access to memory
addresses, enabling powerful features such as dynamic memory allocation, array
manipulation, and efficient parameter passing to functions.
This chapter explores:
🔍 What is a Pointer?
A pointer is a variable that stores the memory
address of another variable.
✅ Syntax:
data_type
*pointer_name;
✅ Example:
int
a = 10;
int
*p = &a;
In this example:
🧾 Pointer Terminology
Symbol |
Meaning |
Example |
* |
Declares a pointer /
dereference |
*ptr, *p = 5 |
& |
Address-of
operator |
&x |
-> |
Access member of
struct via pointer |
ptr->name |
🔢 Declaring and Using
Pointers
✅ Declare a pointer:
int
*ptr;
✅ Assign address to pointer:
int
x = 5;
ptr
= &x;
✅ Dereference to access value:
printf("%d",
*ptr); // Output: 5
📋 Table: Pointer Examples
Code |
Description |
int *p; |
Declares a pointer to
int |
p = &a; |
Stores
address of a in pointer |
*p = 20; |
Changes value at the
address pointed |
printf("%p", p); |
Prints
address |
printf("%d",
*p); |
Prints value at
address |
🔁 Pointer Arithmetic
Pointer arithmetic depends on data type size.
int
a = 10, b = 20;
int
*p = &a;
p++; // now points to next int (typically +4
bytes)
Operation |
Meaning |
p++ |
Move to next memory
address |
p-- |
Move to
previous address |
p + i |
Move forward by i
elements |
p - i |
Move backward
by i elements |
🧰 Pointers and Arrays
An array name is essentially a constant pointer.
int
arr[3] = {10, 20, 30};
int
*ptr = arr;
printf("%d",
*(ptr + 1)); // Output: 20
You can iterate using pointer arithmetic:
for
(int i = 0; i < 3; i++) {
printf("%d ", *(ptr + i));
}
🔁 Pointer to Pointer
(Double Pointer)
A pointer that stores the address of another pointer.
int
a = 10;
int
*p = &a;
int
**q = &p;
printf("%d",
**q); // Output: 10
📦 Pointers and Functions
✅ Passing by reference using
pointers:
void
update(int *x) {
*x = *x + 10;
}
int
main() {
int num = 5;
update(&num);
printf("%d", num); // Output: 15
}
This allows us to modify the original variable inside a
function.
🔃 Dynamic Memory
Allocation
Dynamic memory allows allocation during runtime using
standard library functions in <stdlib.h>.
✅ malloc() – Memory Allocation
int
*p = (int*) malloc(sizeof(int) * 5);
✅ calloc() – Contiguous
Allocation
int
*p = (int*) calloc(5, sizeof(int));
✅ realloc() – Resize Memory Block
p
= (int*) realloc(p, sizeof(int) * 10);
✅ free() – Free Memory
free(p);
📋 Comparison Table –
Memory Allocation Functions
Function |
Initializes
Memory? |
Resizable |
Use Case |
malloc |
No |
Yes (via realloc) |
When performance is
key |
calloc |
Yes (zero-filled) |
Yes |
When
initializing data matters |
realloc |
NA |
Yes |
To expand/shrink
memory block |
free |
NA |
No |
To avoid
memory leaks |
⚠️ Common Pointer Issues
🔹 Dangling Pointer
int
*ptr = (int*) malloc(sizeof(int));
free(ptr);
// Now ptr is dangling; points to deallocated memory
🔹 Memory Leak
Memory allocated but never freed.
int
*p = (int*) malloc(100);
p
= NULL; // Leak! Previous memory is lost
🔹 Null Pointer
Dereference
int
*p = NULL;
printf("%d",
*p); // Crash
🧪 Sample Program: Dynamic
Array Input
#include
<stdio.h>
#include
<stdlib.h>
int
main() {
int n, *arr;
printf("Enter size of array: ");
scanf("%d", &n);
arr = (int*) malloc(n * sizeof(int));
printf("Enter elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("You entered:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
🧠 Real-World Use Cases
Feature |
Application |
Pointer |
Low-level device programming |
Dynamic Memory |
Data
structures (linked list, tree, etc.) |
Memory Management |
Embedded systems,
real-time applications |
📋 Chapter Summary
Topic |
Key Point |
Pointer Basics |
Stores address of
variables |
Dereferencing |
Access value
via * |
Pointer Arithmetic |
Move between memory
locations |
Function Pointers |
Pass by
reference |
Dynamic Allocation |
Use malloc, calloc,
free |
Errors |
Avoid memory
leaks and dangling pointers |
A: Absolutely. C is widely used in systems programming, embedded systems, and performance-critical applications.
A: Just basic computer literacy. No prior programming knowledge is required.
A: Code::Blocks or VS Code with a C plugin is great for beginners.
A: C is a compiled language. It uses compilers like GCC or Clang.
A: With consistent practice, 4–8 weeks is sufficient to grasp core concepts.
A: Pointers and manual memory management can be tricky for beginners.
A: C is not typically used for web apps, but it can handle back-end processes or be integrated via CGI.
A: Each has its use. C is great for low-level control and speed, but C++ and Python offer more abstraction and ease of use.
A: Use a terminal/IDE to compile with gcc filename.c -o output and run with ./output.
A: GitHub, HackerRank, and open-source forums are great places to find beginner to advanced C projects.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)