[d] arrays - [028] arrays in c
What is the index of the first element in an array?
0
What is a linear search in an array?
A search algorithm that checks each element in the array sequentially until the target element is found or the end is reached.
What is an array in C?
An array is a collection of elements of the same data type stored in contiguous memory locations.
How do you pass an array to a function in C?
By passing the array's name, e.g., `functionName(arr);`
What happens if you try to access an array element out of its bounds?
It leads to undefined behavior, potentially causing a program crash or unexpected results.
What is the key difference between static and dynamic arrays in C?
Static arrays have a fixed size defined at compile time, while dynamic arrays can be resized at runtime using dynamic memory allocation functions like `malloc` and `realloc`.
What is a common way to traverse an array?
Using a for loop: `for(int i = 0; i < size; i++) { /* code */ }`
How do you access the third element in an array `arr`?
`arr[2];`
How do you declare an array of strings in C?
`char arr[3][10];` for 3 strings with a maximum length of 9 characters each
How do you free a dynamically allocated array?
`free(arr);`
How do you initialize a 2D array with values in C?
`int arr[2][2] = {{1, 2}, {3, 4}};`
How do you declare a 2D array with 3 rows and 4 columns?
`int arr[3][4];`
How do you initialize an integer array of size 5 with values 1, 2, 3, 4, 5?
`int arr[5] = {1, 2, 3, 4, 5};`
How do you declare an integer array of size 5 in C?
`int arr[5];`
What is the syntax to dynamically allocate an array using `malloc`?
`int* arr = (int*)malloc(n * sizeof(int));`