Advanced Search
Search Results
70 total results found
1. Introduction: From Python Lists to C Arrays
1.1 Key Differences Overview Aspect Python Lists C Arrays Size Dynamic (can grow/shrink) Fixed size (static) Type Can store mixed types Single type only Memory Automatic management Manual bounds checking Declaration list = [1, 2, 3] int arr[5] = {...
2. Array Declaration and Initialization
2.1 Basic Array Declaration Python vs C Comparison: Python C numbers = [1, 2, 3, 4, 5] int numbers[5] = {1, 2, 3, 4, 5}; grades = [] float grades[100]; name = "Alice" char name[10] = "Alice"; C Array Declaration Syntax: data_type array_name[size]...
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 ...
New Page
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...