Advanced Search
Search Results
69 total results found
3. Array Indexing and Access
3.1 Basic Indexing Python vs C Indexing: Operation Python C First element list[0] array[0] Last element list[-1] array[size-1] Nth element list[n] array[n] Modify element list[0] = 10 array[0] = 10; 3.1.1 Valid Indexing Example int numbers[5] =...
4. Array Input and Output Operations
4.1 Reading Array Elements 4.1.1 Reading with Known Size #include <stdio.h> int main() { int numbers[5]; printf("Enter 5 integers:\n"); for (int i = 0; i < 5; i++) { printf("Element %d: ", i + 1); scanf("%d", &numbers[i]); ...
5. Common Array Operations
5.1 Array Traversal 5.1.1 Forward Traversal // Python equivalent: for item in list: for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } 5.1.2 Reverse Traversal // Python equivalent: for item in reversed(list): for (int i = size - 1; i >= 0; i--) { ...
6. Mathematical Operations on Arrays
6.1 Statistical Operations 6.1.1 Sum and Average #include <stdio.h> int sum_array(int arr[], int size) { int sum = 0; for (int i = 0; i < size; i++) { sum += arr[i]; } return sum; } double average_array(int arr[], int size) { if (...
7. Character Arrays and Strings
7.1 Character Arrays vs Strings Understanding C Strings: In C, strings are arrays of characters terminated by a null character ('\0'). // Character array (not necessarily a string) char letters[5] = {'H', 'e', 'l', 'l', 'o'}; // String (null-terminated charac...
8. Multi-dimensional Arrays
8.1 Two-Dimensional Arrays 8.1.1 Declaration and Initialization // Declaration int matrix[3][4]; // 3 rows, 4 columns // Initialization methods int grid[2][3] = { {1, 2, 3}, {4, 5, 6} }; // Alternative initialization int numbers[2][3] = {{1, 2, 3}, ...
1. Introduction to Pointers
1.1 What are Pointers? A pointer is a variable that stores the memory address of another variable. Instead of holding a data value directly, a pointer "points to" the location in memory where the data is stored. Analogy: Think of computer memory like a street ...
2. Pointer Basics
2.1 Declaring Pointers Syntax: data_type *pointer_name; Examples: int *ptr; // Pointer to an integer float *fptr; // Pointer to a float char *cptr; // Pointer to a character double *dptr; // Pointer to a double Important Notes: The * (ast...
3. Pointer Arithmetics
3. Pointer Arithmetic Pointers can be incremented, decremented, and compared. When you perform arithmetic on pointers, the operation takes into account the size of the data type being pointed to. 3.1 Basic Pointer Arithmetic Operations Operation Description...
4. Pointers and Arrays
Arrays and pointers have a very close relationship in C. In many contexts, an array name acts as a pointer to its first element. 4.1 Array Name as Pointer #include <stdio.h> int main() { int arr[5] = {10, 20, 30, 40, 50}; // Array name is a point...
5. Pointers and Functions
Pointers are essential for functions to modify variables from the calling code and to work efficiently with arrays. 5.1 Pass by Value vs Pass by Reference Pass by Value (Without Pointers): #include <stdio.h> void tryToChange(int x) { x = 100; // Only cha...
6. Pointers and Strings
In C, strings are arrays of characters, so pointers work naturally with strings. 6.1 String Representation char str1[] = "Hello"; // Array notation char *str2 = "Hello"; // Pointer notation // Both represent the same thing in memory: // 'H' 'e' 'l' '...
7. Dynamic Memory Allocation & Array
7.1 Introduction to Dynamic Memory Up until now, we've used static memory allocation where array sizes must be known at compile time: int arr[100]; // Size fixed at compile time Problems with static allocation: Waste memory if you allocate too much Run out ...
8. Common Pointer Pitfalls and Best Practices
8.1 Uninitialized Pointers WRONG: int *ptr; // Uninitialized - contains garbage address *ptr = 42; // DANGER! Writing to unknown memory location // May cause segmentation fault CORRECT: int *ptr = NULL; // Initialize to NULL int num ...
9. Practical Examples with Dynamic Memory
9.1 Dynamic Array with User Input #include <stdio.h> #include <stdlib.h> int main() { int n, *arr; printf("How many numbers do you want to enter? "); scanf("%d", &n); // Allocate memory arr = (int*)malloc(n * sizeof(int)); if...
1. Introduction to User-Defined Data Types
1.1 Why User-Defined Types? In previous modules, we learned about basic data types like int, float, char, etc. These are sufficient for simple programs, but real-world applications often require more complex data structures. Example Problem: Suppose you want t...
2. Structures (struct)
2.1 What is a Structure? A structure is a user-defined data type that groups variables of different types under a single name. Think of it as creating your own custom data type. Python vs C Comparison: Python C Uses classes or dictionaries Uses struct ...
3. Enumerations (enum)
3.1 What is an Enumeration? An enumeration is a user-defined data type consisting of a set of named integer constants. Enums make code more readable by replacing "magic numbers" with meaningful names. Python vs C Comparison: Python C No built-in enum (u...
4. Type Definitions (typedef)
4.1 What is typedef? typedef creates aliases (alternative names) for existing data types. It makes code more readable and easier to maintain. Basic Syntax: typedef existing_type new_name; 4.2 typedef with Basic Types // Create aliases for basic types typedef ...
5. File Input/Output
5.1 Why File I/O? So far, all our programs lose their data when they terminate. File I/O allows programs to: Save data permanently Read data from external sources Create logs and reports Share data between programs Python vs C File Operations: Python C ...