Two-Dimensional Arrays
Declaration
The syntax for declaring a two-dimensional array is: storage_class data_type array_name[rows][columns];
int matrix[2][3] = {1, 2, 3, 4, 5, 6}; // Declares a 2x3 integer matrix
Accessing Elements
Elements are accessed using array_name[row_index][column_index], where indices start from zero.
Note: Both row and column indices must be within the array bounds. When declaring, the number of rows can be omitted, but the number of columns cannot.
Size and Element Count
- Element Count: The total number of elements is
rows * columns. This can also be calculated assizeof(array_name) / sizeof(data_type). - Total Size: The total memory size of the array is
sizeof(data_type) * rows * columns, which is equivalent tosizeof(array_name).
Key Points:
array_namerefers to the base address of the array.array_name + 1points to the beginning of the second row.array_name[0]refers to the base address of the first row.array_name[0][0]refers to the address of the first element in the first row.
Initialization
- Full Initialization:
int matrix[2][3] = {1, 2, 3, 4, 5, 6}; // Sequential assignment int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; // Row-wise assignment - Partial Initialization: Uninitialized elements are set to zero.
int matrix[2][3] = {1, 2, 3, 4}; // Fills first 4 elements, rest are 0 int matrix[2][3] = {{1, 3}, {4}}; // Fills {1, 3, 0}, {4, 0, 0} - No Initialization: Elements will contain indeterminate values.
int matrix[2][3]; // Contains garbage values matrix[0][0] = 1; // Individual element assignment
Memory Traversal
When traversing a two-dimensional array like matrix[2][4], incrementing the array name (matrix + 1) moves the pointer to the beginning of the next row, not just to the next element.
Pointers
Advantages
- Enhance program concisaness, compactness, and efficiency.
- Enable the representation of complex data structures.
- Facilitate dynamic memory allocation for efficient space utilization.
- Allow functions to return multiple values.
Core Concepts
- Address: A unique identifier (number) for a memory location.
- Pointer: A varible that stores a memory address.
- Pointer Variable: A variable specifically designed to hold memory addresses.
Declaration
The syntax for declaring a pointer variable is: storage_class data_type *variable_name;
int *ptr; // Declares a pointer variable named ptr
Note: The data type of a pointer indicates the type of data it is expected to point to.
Pointer and Variable Relationship
*ptraccesses the value stored at the addressptrpoints to.ptrholds the memory address.
int variable = 10;
int *ptr = &variable;
// *ptr will have the value 10
// ptr will hold the address of 'variable'
Pointer Operators
&: Address-of operator (retrieves the memory address of a variable).*: Dereference operator (accesses the value at the memory address).
Examples:
&ptr: Address of the pointer variableptritself.*&variable: Equivalent tovariable.&*variable: Incorrect usage.
Initialization
- Pointing to a regular variable:
int value = 0; int *pointer = &value; - Pointing to an array: A pointer to the first element of an array.
int arr[5] = {10, 20, 30, 40, 50}; int *pointer = arr; // Equivalent to int *pointer = &arr[0]; - Assigning one pointer to another: Copying the address held by one pointer to another.
int a = 0; int *p1 = &a; int *p2 = p1;
Pointer Arithmetic
Pointer arithmetic calculates new addresses based on the data type the pointer points to. Incrementing/decrementing a pointer moves it by multiples of the size of its base data type.
pointer + n: Moves the pointernelements forward in memory (towards higher addresses). The pointer's own address does not change, but its target address does.pointer - n: Moves the pointernelements backward in memory (towards lower addresses).pointer++: Increments the pointer to point to the next element of its data type.pointer--: Decrements the pointer to point to the previous element of its data type.
Example:
int *ptr; ptr++;// Moves the pointer forward bysizeof(int)bytes (typically 4).char *ptr; ptr++;// Moves the pointer forward bysizeof(char)byte (typically 1).