A comprehensive collection of C programs demonstrating fundamental concepts including pointers, arrays, dynamic memory allocation, functions, and structures.
This repository contains practical examples and exercises for learning C programming, with a strong focus on pointer manipulation, memory management, and data structures. Each program is designed to demonstrate specific concepts with clear, commented code.
c_prog/
βββ pointer_basics/ # Introduction to pointers
βββ pointer_arithmetic/ # Pointer arithmetic operations
βββ pointer_array/ # Pointers with arrays (1D & 2D)
βββ Dynamic Memory Allocation/ # malloc, calloc, and free
βββ function/ # Functions with pointers
βββ structure/ # Structures and pointers
βββ try.c # Quick pointer experiments
- Understanding pointer declaration and initialization
- Accessing values through pointers (dereferencing)
- Printing addresses and values
- Pointer size vs data type size
- Incrementing and decrementing pointers
- Address calculation based on data types
- Understanding memory offsets
- Array-pointer relationship
- Traversing arrays using pointers
- 2D arrays with pointers
- Character pointer arrays (strings)
- Array reversal using pointers
- Character frequency counter
malloc()- Memory allocationcalloc()- Contiguous allocation with initializationfree()- Memory deallocation- Runtime array size determination
- Pass by reference
- Pointer parameters in functions
- Array manipulation through functions
- String processing functions
- Structure definition and initialization
- Pointer to structure
- Arrow operator (
->) usage - Structure memory layout
gcc filename.c -o outputname./outputnamecd pointer_basics
gcc 1.c -o pointer_demo
./pointer_demoConcepts: Pointer declaration, address-of operator (&), dereference operator (*)
// Demonstrates:
// - Declaring integer pointer
// - Storing address of variable
// - Accessing value through pointer
// - Printing addresses and valuesOutput: Displays value of variable, its address, pointer value, and dereferenced pointer value
Concepts: Pointer increment/decrement, sizeof operator, address calculation
// Demonstrates:
// - Pointer increment (p++)
// - Address changes based on data type
// - Size of integer vs size of pointer
// - Pointer subtractionKey Learning: When you increment a pointer, it moves by sizeof(data_type) bytes
Concepts: Array-pointer equivalence, pointer increment, sequential access
// Demonstrates:
// - Array name as base address
// - Manual pointer increment
// - Accessing array elements via pointerOutput: Displays each array element with its memory address
Concepts: Efficient array traversal using while loop
// Demonstrates:
// - Loop-based pointer traversal
// - Cleaner code structure
// - Same concept as array.c but optimizedConcepts: 2D arrays, pointer to array, character pointer arrays
// Demonstrates:
// - 2D array declaration
// - Pointer to 2D array
// - Array of string pointers
// - String storage and accessOutput: Displays sports names with their memory locations
Concepts: String traversal, character comparison, function with pointer parameter
// Demonstrates:
// - String pointer traversal
// - Character frequency counting
// - Null terminator check
// - Practical string processingOutput: Counts occurrences of character 's' in the phrase "She sells sea shells in the sea shore"
Concepts: Reverse traversal, pointer arithmetic, array length calculation
// Demonstrates:
// - Forward array traversal
// - Reverse array traversal
// - Pointer to last element (a + len - 1)
// - Function to reverse displayOutput: Shows array in normal and reverse order with addresses
Concepts: Dynamic memory allocation, runtime array size, memory deallocation
// Demonstrates:
// - User input for array size
// - malloc() for dynamic allocation
// - Array initialization and access
// - free() to prevent memory leaksKey Learning: malloc() allocates uninitialized memory
Concepts: Contiguous allocation with zero initialization
// Demonstrates:
// - calloc() vs malloc()
// - Automatic zero initialization
// - Dynamic array operationsKey Learning: calloc() initializes allocated memory to zero
Concepts: Pointer parameters, pass by reference, function return values
// Demonstrates:
// - Passing addresses to functions
// - Dereferencing in function
// - Addition using pointersOutput: Sum of two numbers using pointer parameters
Concepts: Array parameter, string parameter, void functions
// Demonstrates:
// - Passing array to function
// - Passing string to function
// - Character-by-character string display
// - While loop with pointer conditionOutput: Displays array elements and string characters
Concepts: Structure definition, pointer to structure, arrow operator
// Demonstrates:
// - Structure declaration
// - Pointer to structure
// - Arrow operator (->)
// - String copy with strcpy()
// - Structure size calculationOutput: Student details (roll number, name, marks) and structure size
Concepts: Pointer basics, pointer arithmetic
// Demonstrates:
// - Basic pointer operations
// - Address increment
// - sizeof() for intPurpose: Quick testing and experimentation with pointer concepts
- Start with
pointer_basics/1.c - Move to
try.cfor basic pointer arithmetic - Explore
pointer_arithmetic/operation.c
- Study
pointer_array/array.candarrayVer2.c - Learn functions with
function/1.candfunction/2.c - Understand structures with
structure/1.c
- Master 2D arrays with
pointer_array/2D.c - Practice string manipulation with
pointer_array/ext.c - Learn dynamic allocation with
Dynamic Memory Allocation/mall.candcall.c - Challenge yourself with
pointer_array/reverseArray.c
| Concept | Syntax | Example |
|---|---|---|
| Pointer Declaration | int *p; |
Declares pointer to int |
| Address-of | &variable |
Gets address of variable |
| Dereference | *pointer |
Gets value at address |
| Pointer Arithmetic | p++ or p+n |
Moves pointer by n elements |
| Array-Pointer | int *p = arr; |
Array name is base address |
| malloc | malloc(size) |
Allocates uninitialized memory |
| calloc | calloc(n, size) |
Allocates zero-initialized memory |
| free | free(pointer) |
Deallocates memory |
| Structure Pointer | ptr->member |
Accesses structure member |
int *p = array;
while (p < array + size) {
printf("%d ", *p);
p++;
}char *p = string;
while (*p != '\0') {
printf("%c", *p);
p++;
}int *p = (int*)malloc(n * sizeof(int));
// Use the memory
free(p);- All programs use standard C libraries (
stdio.h,stdlib.h,string.h) - Compiled executables and debug symbols (
.dSYM) are included - Programs are designed for educational purposes with clear demonstrations
- Memory is properly deallocated in dynamic allocation examples
To extend your learning:
- Add error handling to dynamic memory allocation
- Implement more complex data structures (linked lists, trees)
- Practice with double pointers and function pointers
- Explore pointer to functions
- Study memory alignment and padding in structures
- C Programming Language by Brian W. Kernighan and Dennis M. Ritchie
- Pointers in C by Yashavant Kanetkar
- Online C documentation and tutorials
Happy Coding! π
This repository is a learning journey through C programming fundamentals. Each program builds upon previous concepts, creating a comprehensive understanding of pointers and memory management in C.