Unit III: Arrays and Pointers
1. Array Processing and Strings
An array is a collection of elements of the same type stored in contiguous memory locations.
- Definition and Processing: Arrays are defined by specifying the data type and the number of elements. Processing involves accessing each element using its index.
- Passing Arrays to Functions: When an array is passed to a function, it is typically passed by reference (the address of the first element is passed).
- Multidimensional Arrays: These are "arrays of arrays," such as matrices (2D arrays) used for grid-based data storage.
- Arrays and Strings: In C, a string is simply a one-dimensional array of characters terminated by a null character (\0).
2. Pointer Fundamentals
A pointer is a variable that stores the memory address of another variable.
- Pointers and Addresses: Every variable has an address in memory; pointers allow direct manipulation of these addresses.
- Pointer Declaration: Pointers are declared using the * operator (e.g., int *p).
- Pointers and Function Arguments: Passing pointers to functions allows the function to modify the actual variable in the calling environment (Pass by Reference).
- Pointers and One-dimensional Arrays: The name of an array acts as a constant pointer to its first element.
3. Address Arithmetic
Address arithmetic refers to the operations that can be performed on pointers.
- Operations: You can increment (p++), decrement (p--), or add/subtract integers to a pointer to navigate through memory.
- Character Pointers and Functions: Character pointers (char *ptr) are frequently used to handle strings and are passed to functions for efficient string manipulation.
Rule: When you increment a pointer, it moves forward by the size of the data type it points to (e.g., +4 bytes for an int pointer on most systems).
4. Pointers vs Multidimensional Arrays
There is a strong relationship between pointers and multidimensional arrays in C.
- Pointer Arrays: An array where each element is a pointer to another variable or array.
- Pointers to Pointers: A variable that stores the address of another pointer (declared as **p).
- Initialization: Pointer arrays can be initialized with addresses of strings or other variables for flexible data structures.
- Pointers and Multidimensional Arrays: Accessing elements like A[i][j] can be represented using pointer notation as *(*(A+i)+j).
5. Command Line Arguments & Function Pointers
Advanced pointer usage allows for dynamic and highly flexible programming.
- Command Line Arguments: These allow parameters to be passed to the program when it starts via
int main(int argc, char *argv[]).
- Pointers to Functions: Pointers can store the address of a function, allowing functions to be passed as arguments to other functions.
- Passing Functions: This is the basis for "callback" mechanisms in C.
Exam Tips
- The & and * Operators: Remember that & is the "Address of" operator, while * is the "Value at address" (dereference) operator.
- Null Pointer: Always initialize pointers to NULL if they aren't assigned an address immediately to avoid "wild pointers."
- Array Bounds: C does not check array boundaries. Accessing A[10] in an array of size 5 is a common logic error.
Frequently Asked Questions
Q: What is the difference between char a[] = "hello" and char *p = "hello"?
The first is an array that can be modified. The second is a pointer to a string literal, which is often stored in read-only memory and should not be modified.
Q: What is 'argc' and 'argv'?
'argc' (argument count) is the number of strings on the command line, and 'argv' (argument vector) is an array of pointers to those strings.