Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

C Programming Learning Repository

A comprehensive collection of C programs demonstrating fundamental concepts including pointers, arrays, dynamic memory allocation, functions, and structures.

Overview

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.

Directory Structure

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

Topics Covered

1. Pointer Basics πŸ“

  • Understanding pointer declaration and initialization
  • Accessing values through pointers (dereferencing)
  • Printing addresses and values
  • Pointer size vs data type size

2. Pointer Arithmetic βž•βž–

  • Incrementing and decrementing pointers
  • Address calculation based on data types
  • Understanding memory offsets

3. Pointers with Arrays πŸ”’

  • Array-pointer relationship
  • Traversing arrays using pointers
  • 2D arrays with pointers
  • Character pointer arrays (strings)
  • Array reversal using pointers
  • Character frequency counter

4. Dynamic Memory Allocation πŸ’Ύ

  • malloc() - Memory allocation
  • calloc() - Contiguous allocation with initialization
  • free() - Memory deallocation
  • Runtime array size determination

5. Functions with Pointers πŸ”§

  • Pass by reference
  • Pointer parameters in functions
  • Array manipulation through functions
  • String processing functions

6. Structures πŸ—οΈ

  • Structure definition and initialization
  • Pointer to structure
  • Arrow operator (->) usage
  • Structure memory layout

Compilation & Execution

Compile a single program:

gcc filename.c -o outputname

Run the compiled program:

./outputname

Example:

cd pointer_basics
gcc 1.c -o pointer_demo
./pointer_demo

Program Descriptions

πŸ“ pointer_basics/

1.c - Introduction to Pointers

Concepts: Pointer declaration, address-of operator (&), dereference operator (*)

// Demonstrates:
// - Declaring integer pointer
// - Storing address of variable
// - Accessing value through pointer
// - Printing addresses and values

Output: Displays value of variable, its address, pointer value, and dereferenced pointer value


πŸ“ pointer_arithmetic/

operation.c - Pointer Arithmetic Operations

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 subtraction

Key Learning: When you increment a pointer, it moves by sizeof(data_type) bytes


πŸ“ pointer_array/

array.c - Basic Array Traversal

Concepts: Array-pointer equivalence, pointer increment, sequential access

// Demonstrates:
// - Array name as base address
// - Manual pointer increment
// - Accessing array elements via pointer

Output: Displays each array element with its memory address


arrayVer2.c - Array Traversal with Loop

Concepts: Efficient array traversal using while loop

// Demonstrates:
// - Loop-based pointer traversal
// - Cleaner code structure
// - Same concept as array.c but optimized

2D.c - Multi-dimensional Arrays & String Arrays

Concepts: 2D arrays, pointer to array, character pointer arrays

// Demonstrates:
// - 2D array declaration
// - Pointer to 2D array
// - Array of string pointers
// - String storage and access

Output: Displays sports names with their memory locations


ext.c - Character Frequency Counter

Concepts: String traversal, character comparison, function with pointer parameter

// Demonstrates:
// - String pointer traversal
// - Character frequency counting
// - Null terminator check
// - Practical string processing

Output: Counts occurrences of character 's' in the phrase "She sells sea shells in the sea shore"


reverseArray.c - Array Reversal

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 display

Output: Shows array in normal and reverse order with addresses


πŸ“ Dynamic Memory Allocation/

mall.c - malloc() Example

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 leaks

Key Learning: malloc() allocates uninitialized memory


call.c - calloc() Example

Concepts: Contiguous allocation with zero initialization

// Demonstrates:
// - calloc() vs malloc()
// - Automatic zero initialization
// - Dynamic array operations

Key Learning: calloc() initializes allocated memory to zero


πŸ“ function/

1.c - Pass by Reference

Concepts: Pointer parameters, pass by reference, function return values

// Demonstrates:
// - Passing addresses to functions
// - Dereferencing in function
// - Addition using pointers

Output: Sum of two numbers using pointer parameters


2.c - Array & String Functions

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 condition

Output: Displays array elements and string characters


πŸ“ structure/

1.c - Structure with Pointers

Concepts: Structure definition, pointer to structure, arrow operator

// Demonstrates:
// - Structure declaration
// - Pointer to structure
// - Arrow operator (->)
// - String copy with strcpy()
// - Structure size calculation

Output: Student details (roll number, name, marks) and structure size


πŸ“„ Root Directory Files

try.c - Quick Pointer Experiments

Concepts: Pointer basics, pointer arithmetic

// Demonstrates:
// - Basic pointer operations
// - Address increment
// - sizeof() for int

Purpose: Quick testing and experimentation with pointer concepts


🎯 Learning Path

Beginner

  1. Start with pointer_basics/1.c
  2. Move to try.c for basic pointer arithmetic
  3. Explore pointer_arithmetic/operation.c

Intermediate

  1. Study pointer_array/array.c and arrayVer2.c
  2. Learn functions with function/1.c and function/2.c
  3. Understand structures with structure/1.c

Advanced

  1. Master 2D arrays with pointer_array/2D.c
  2. Practice string manipulation with pointer_array/ext.c
  3. Learn dynamic allocation with Dynamic Memory Allocation/mall.c and call.c
  4. Challenge yourself with pointer_array/reverseArray.c

πŸ’‘ Key Concepts Summary

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

πŸ” Common Patterns

Array Traversal Pattern

int *p = array;
while (p < array + size) {
    printf("%d ", *p);
    p++;
}

String Traversal Pattern

char *p = string;
while (*p != '\0') {
    printf("%c", *p);
    p++;
}

Dynamic Allocation Pattern

int *p = (int*)malloc(n * sizeof(int));
// Use the memory
free(p);

πŸ“ Notes

  • 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

πŸš€ Next Steps

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

πŸ“– Resources

  • 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.

About

C_files

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages