1/49
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Declare an array called candy with 365 pieces.
float candy[365];
// type, name, sizeInitialize some of the values in candy using a comma separated list/initializer list
float candy[365] = {1,3,5,6,8,20};What happens when you declare an array as const?
Const arrays are meant to be read-only. This means they must be initialized and declared at the same time, and after initialization, they can no longer be assigned to.
Declare a const array called days. It should have a size of MONTHS.
const int days[MONTHS];What happens when you only partially initialize an array?
int some_data[4] = {1,2,3,4};It will automatically fill the rest with 0.
What happens if you have too many initializers?
int a[3] = {13,17,100,95,63};C does not allow more initializers than the array size. Doing this will produce a compiler error.
What happens if you omit the size of the array?
int a[] = {3,5,8,9};The compiler will automatically set the array size for you based on the number of elements.
Write a declaration for an array of 5 integers initialized to 10,20,30,40,50.
int candy[5] = {10,20,30,40,50};What can you say about the values in the array: int x[3]
They contain garbage
Which of these is valid
Answer: const int a[3] = {1,2,3};
This is because const arrays must always be initialized at the time of declaration.
What does sizeof do?
sizeof returns the amount of storage in bytes.
What would this give us, assuming that days is an entire array of 1ints:
sizeof days / sizeof days[0]
sizeof days would return the total size of the array in bytes
sizeof days[0] would return the size of the very first element in bytes
Each int in the array is 4 bytes. This means sizeof days = 4 × 10 = 40 bytes. Therefore sizeof days[0] = 4 bytes. So the expression becomes: 40/4 = 10.
10 represents the total number of elements in the array.
Predict the output of the following program:
#include <stdio.h>
int main(void) {
const int days[] = {31,28,22};
int index;
for(index = 0; index < sizeof days / sizeof days[0]; index++) {
printf("Month: %2d has %d days.\n", index+1, days[index]);
}
return 0;
}The for loop currently looks like this: index = 0, index < 3, index++
so the output will be:
Month: 1 has 31 days.
Month: 2 has 28 days.
Month: 3 has 22 days.
What does a designated initializer look like? How is it different from a traditional initializer?
A designated initializer looks like this: int arr[6] = { [5] = 212 };
A traditional initializer looks like this: int arr[6] = {0,0,0,0,12};
The difference is that designated initializers only set particular elements, not all of them. Any elements not assigned to are left as 0.
What are the values in the following array:
int days[MONTHS] = {31,28,[4]=30,28,18,[1]=29};days[0] = 31
days[1] = 28
days[4] = 30
days[5] = 28
days[6] = 18
days[1] = 29 // we overwrote the previous value
What ends up being the size of this array?
int staff[] = {1, [6]=4, 9, 10};The size of the array ends up being 9, because there are 8 indices specified in the actual array.
Can you assign one array to another in C?
No, C does not support array to array assignment.
Are you allowed to assign values out of the bounds of an array?
Yes you are allowed. This is because C does not check the bounds of arrays. Even if you assign something out of bounds, C will not warn you about it.
What is the size of this array?
int nums[] = {4, 8, 15, 16, 23, 42};
Size: 6
What does this expression evaluate to?
double data[] = {1.1,2.2,3.3};
sizeof data / sizeof data[0];
The expression evaluates to: 24 / 8 = 3
Note: Each double is 8 bytes in size
What are the values in the following array?
int a[6] = {[3]=10,20};
All values in the array are 0 except for: a[3] = 10, and a[4] = 20
What will happen in the following assignment:
int x[3] = {1,2,3};
x[3] = 10;
This will result in undefined behavior, but C will not warn you about it, and will let you do it anyway.
Is this allowed?
float a4[-3];No
Is this allowed?
float a2[5*2+1]
Yes
Is this allowed?
float a3[sizeof (int) + 1];
Yes
Is this allowed?
float a7[(int) 2.5];
Yes
Is this allowed?
float a5[0];
No
Is this allowed?
float a7[2.5];
No
What are variable-length arrays?
Arrays that use variables as their size.
What is a unique feature about variable-length arrays?
They cannot be static.
How many elements does this array contain?
float m[3][4]
12 elements
Which element comes immediately after rain[1][11] in memory?
Note that rain is a 5 by 12 array: rain[12][5]
rain[2][0]
Write the loop structure needed to sum up all elements of this 2D Array:
a[R][C]
int sum = 0;
for(int i=0; i < R; i++) {
for(int k=0; k < C; k++) {
sum += a[i][k];
}
}
Describe what this 3D array declaration actually looks like:
int box[2][3][4]box is an array of 2 elements,.
each element is an array of 3 elements.
each of those 3 elements is an array of 4 ints.
Therefore, we have:
{element 1, element 2}
Then:
{ {x,x,x}, {x,x,x} }
Then:
{ {{y, y, y, y}, {y,y,y,y}, {y,y,y,y}} }, {{{y, y, y, y}, {y,y,y,y}, {y,y,y,y}} } }
What happens when you assign a pointer to an array directly?
int *p = arr;
The pointer decays to a pointer to the very first element in the array: arr[0]
If pt1 is a pointer to an array of short, what does this do?
printf("%d\n", pt1+1);pt1 increases by 2 bytes, because 2 bytes is the size of a short. It essentially moves in the size of the pointed to type, effectively moving it one element over.
If ptf is a pointer to an array of doubles, what does this do?
printf("%d\n", ptf+1);It moves the pointer over by 8 bytes (the size of a double), moving it to the next element.
Are these two equivalent statements?
*(dates+2) == &dates[2]
No, the first is referring to the value at a certain pointer position, while the other is referring to an address.
Are these two equivalent statements?
dates+2 == &dates[2]
Yes.
This is because dates is originally a pointer to dates[0]. So adding 2 to it moves it over to dates[2].
&dates[2] has the exact same meaning as the pointer. It means the address at dates[2].
Are these two statements equivalent?
*(dates+2) == dates[2];Yes, we are getting the value at dates[2] and comparing it to the value at dates[2]. These are exactly the same.
What is different between the two of these:
*dates+2 and *(dates+2)
*dates+2 means dereference (or get the value at) dates[0], and add 2 to that value.
*(dates+2) means dereference (or get the value at) dates[2]
What format specifier is used for pointers?
%p is used for pointers
What happens when you use arrays inside of function calls?
Example:
total = sum(marbles);
In most expressions, including function calls, the array name decays to a pointer to the very first element. So now marbles is of type int *.
If ar is a pointer known as: int *ar
Is it okay for us to use ar[i] to refer to it in expressions?
Yes. Although ar is a pointer, ar[i] is defined as *(ar+i)
Are these expressions interchangeable?
int sum(int ar[], int n)
int sum(int *ar, int n)
They are only interchangeable only within function parameter lists, nowhere else.
If int a[5] and int *p = a, what is the relationship between a+2 and &a[2]
These are equivalent.
a+2 means &a[0]+2 = &a[2]
this is the same as &a[2]
Write a complete C program that declares an array of 5 shorts: short nums[5] = {10,20,30,40,50};
Declare a pointer p to short, assign the array to the pointer, use pointer arithmetic to print each element.
#include <stdio.h>
int main(void) {
short nums[5] = {10,20,30,40,50};
short *p = nums;
for(int i=0; i < sizeof nums / size of nums[0]; i++) {
printf("%d", *(p+i));
}
return 0;
}Write a complete program that declares an array of 6 integers, fills the array of values using array indexing: arr[i]…. Prints the values using pointer notation, then prints the same values again using array notation: arr[i].
#include <stdio.h>
int main(void) {
int arr[6] = {1,2,3,4,5,6};
int *ptr = arr;
for(int i=0; i < sizeof arr/sizeof arr[0]; i++) {
arr[i] = i*2;
printf("%d ", *(ptr+i));
printf("%d ", arr[i]);
}
return 0;
}Write a complete program that declares an array of 8 integers, writes a function int sum(int *ar, int n). Inside the function, use pointer arithmetic to compute the sum. Call the function in main and print the result.
#include <stdio.h>
int sum(int *ar, int n);
int main(void) {
int arr[8] = {1,2,3,4,5,6,7,8};
printf("The sum is: %d", sum(arr, 8));
return 0;
}
int sum(int *ar, int n) {
int sum = 0;
for(int i=0; i < n; i++) {
sum += *(ar+i);
}
return sum;
}Write the same program from the previous question, but change the function header to int sum(int ar[], int n). Use pointer arithmetic inside the function.
News flash: Nothing actually changes about the previous function or code. It stays exactly the same. Remember: int sum(int ar[], int n) is equivalent to int sum(int *ar, int n), but ONLY within a function prototype.