C Arrays & Pointers: quick revision, 19 bites
All 19 revision bites from this kit, free and with no sign-up.
Arrays and Pointers: The Dynamic Duo
Arrays and pointers work together in C.
In C, an **array** is a collection of elements of the same type stored in contiguous memory locations. A **pointer** stores the memory address of a variable. The array name itself acts as a pointer to the first element, making them closely related. Understanding this relationship is key to efficient C programming.
Takeaway. Arrays and pointers are two sides of the same coin in C.
1D Array Declaration and Initialization
Declare and initialize arrays easily.
A **1D array** is declared with `type array_name[size];`. For example, `int marks[5];` creates an array of 5 integers. You can initialize at declaration: `int marks[5] = {90, 85, 78, 92, 88};`. If fewer values are given, the rest are zero-initialized.
Takeaway. 1D arrays store a fixed-size sequence of elements of the same type.
Multi-dimensional Arrays
Tables and matrices in C.
A **multi-dimensional array** is an array of arrays. For example, `int matrix[3][3];` creates a 3x3 matrix. It is stored in row-major order, meaning rows are stored contiguously. Access elements with `matrix[row][col]`.
Takeaway. Multi-dimensional arrays are arrays of arrays, useful for matrices and tables.
Variable Length Arrays (VLAs)
Arrays with size decided at runtime.
A **Variable Length Array (VLA)** allows the array size to be determined at runtime, not compile time. For example, `int n; scanf("%d", &n); int arr[n];`. This is useful when the size is not known in advance. However, VLAs have limitations: they cannot be initialized at declaration and may cause stack overflow if too l…
Takeaway. VLAs provide flexibility but require careful memory management.
Flexible Array Members
Arrays that adapt to structure size.
A **flexible array member** is the last member of a structure that can have an array of unspecified size. It allows the structure to have a variable-length array at the end. For example, `struct Student { int id; float marks[]; };`. The array size is determined when memory is allocated.
Takeaway. Flexible array members enable structures with variable-size data.
Advanced Control Flow: goto, NULL, Comma Operator
Control flow beyond loops and conditions.
The **goto** statement jumps to a labeled statement. The **NULL statement** is a semicolon alone, doing nothing. The **comma operator** evaluates multiple expressions, returning the last. These are advanced tools that can simplify code but also make it less readable.
Takeaway. goto, NULL, and comma operator offer alternative control flow but should be used sparingly.
setjmp and longjmp for Non-local Jumps
Jump out of deep function calls.
`setjmp` and `longjmp` are used for **non-local jumps**, allowing you to jump from a deeply nested function back to a saved point. `setjmp` saves the environment, and `longjmp` restores it. This is useful for error handling, but it bypasses normal function return and can cause resource leaks.
Takeaway. setjmp/longjmp enable non-local jumps but require careful cleanup.
Pointer Initialization and Dereferencing
Pointers store addresses, not values.
A **pointer** is declared with `type *ptr;`. Initialize it with the address of a variable using `&`. **Dereference** with `*` to access the value at the address. For example, `int x = 10; int *p = &x; printf("%d", *p);` prints 10.
Takeaway. Pointers store addresses; dereferencing accesses the value.
Special Pointer Types: NULL, Void, Wild, Dangling
Pointers have special states to know.
**NULL pointer** points to nothing (address 0). **Void pointer** (`void *`) can point to any type but must be cast before dereferencing. **Wild pointer** is uninitialized. **Dangling pointer** points to memory that has been freed. Avoid wild and dangling pointers.
Takeaway. Know the special pointer types to avoid bugs.
Double Pointers (Pointer to Pointer)
Pointers that point to pointers.
A **double pointer** stores the address of another pointer. Declared as `int **pp;`. It is useful for modifying pointer values in functions and for 2D arrays. For example, `int x = 5; int *p = &x; int **pp = &p;` then `**pp` gives 5.
Takeaway. Double pointers allow indirect access and modification of pointers.
Function Pointers and Pointers to Functions
Store and call functions via pointers.
A **function pointer** stores the address of a function. Declare as `return_type (*ptr)(parameters);`. Assign `ptr = function_name;` and call via `ptr(args);`. This enables callbacks and dynamic function selection.
Takeaway. Function pointers allow functions to be passed as arguments.
Multilevel Pointers
Pointers to pointers to pointers...
**Multilevel pointers** are pointers that point to other pointers. For example, `int ***p;` is a pointer to a pointer to a pointer. They are used in complex data structures and dynamic memory allocation. Each level adds another indirection.
Takeaway. Multilevel pointers add layers of indirection, useful for complex structures.
Practical: Store and Analyze Marks
Use arrays and pointers for real data.
A common task is to store marks of N students using an array and use pointers to calculate average, highest, and lowest. This demonstrates array traversal with pointers.
Takeaway. Pointers simplify array processing and analysis.
Practical: Matrix Operations with 2D Arrays
Perform matrix math using pointers.
Matrix addition, subtraction, and multiplication can be implemented using 2D arrays and pointer arithmetic. This is a classic application of multi-dimensional arrays.
Takeaway. 2D arrays and pointers enable matrix computations.
Practical: Dynamic Memory with malloc()
Allocate memory at runtime.
Use `malloc()` to allocate memory dynamically. For example, store employee salaries and compute total and highest. This requires pointer arithmetic and careful memory management.
Takeaway. Dynamic memory allocation allows flexible data storage.
Practical: Pointer to Pointer for Modifying Values
Modify variables through multiple levels.
Double pointers allow functions to modify the pointer itself. This is useful for linked lists and dynamic data structures.
Takeaway. Double pointers enable modifying pointer values in functions.
Common Mistakes with Arrays and Pointers
Avoid these pitfalls.
Takeaway. Be careful with memory and bounds to avoid crashes.
Exam Tips for Arrays and Pointers
Score well with these tips.
Takeaway. Practice pointer problems to ace exams.
Memory Trick: Pointer to Array
Remember: array name is a pointer.
Think of an array name as a constant pointer to its first element. So `arr` is like `&arr[0]`. This helps remember that `arr[i]` is equivalent to `*(arr + i)`.
Takeaway. Array indexing is just pointer arithmetic in disguise.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages