APS105 L13 - Arrays
#APS105 Slides Lecture Link to web notes
Basic Array Syntax in C
Arrays in C are similar to in other languages. They are declared as follows:
<dataType> arrayName[<arraySize>];
<dataType>is the data type of values stored in the array, such asintordouble. In other words, the values must not differ in terms of data type, at least using the standard array info taught in this course.arrayNameis the name of the array you are setting. It is considered a pointer to specific elements in the array. I don't exactly know why though?[<arraySize>]dictates the number of items that can fit in the array. Note that this is not expressed in base 0, so an array size of2would include indices of0and1, and NOT2.
...and then individual values are added to the array at specified indices using the syntax as follows:
arrayName[<index>] = <value>arrayName[<index>]expresses the exact index that the corresponding value should be set to in the array.<value>is the value that you want to set at the specified index in the array.[1] Note that this must correspond with the data type of the array!
Example: Creating an array and adding values to it
int arrayInt[2]; // creates an int array with size 2
double arrayDouble[3]; // creates a double array with size 3
arrayInt[0] = 123; // sets element at index 0 in array
arrayDouble[1] = 0.123; // sets element at index 1 in array
Example: Using an array of grades to calculate grade average
int main(void) {
int grades[5]; // creating an array of integers with size 5
grades[0] = 75;
grades[1] = 83;
grades[2] = 99;
grades[3] = 64;
grades[4] = 72;
int sum = 0;
for (int i = 0; i < 5; ++i) {
sum += grades[i];
}
int average = sum / 5;
printf("Average: %d\n", average);
return EXIT_SUCCESS;
}
Using #define to Simplify Array Definitions
Use #define at the beginning of a C program, where the #include is, to essentially set a "variable" that isn't actually used by the program in the same was a normal variable, but can be accessed by any function in the program. You can use it to define the length of an array. The syntax is as follows:
#define SEARCH_STRING <replacement>
SEARCH_STRINGis the string you want to replace, written in all caps.<replacement>is the value you want to replace it with instead.
You can also treat#defineas a "function" by passing in values through().
#define SEARCH_STRING(<value>) (<replacement>)SEARCH_STRING(<value>)takes in a<value>, and the output of that will depend on the logic in the<replacement>.<replacement>is some logic that depends on the inputted<value>.
Using CSV for Creating an Array in a Single Line
You can also create an array AND set the values at its indices by using the following syntax:
<type> name[<array_size>] = {<csv>}
<type>is the variable type of the items in your listnameis the variable name of the list<array_size>is the intended size of the array<csv>is a comma separated values list
Example: Using an array, defined with a CSV, to calculate grade average.
#include <stdio.h>
#define GRADES_LENGTH 5 // length of the array set at beginning of program
int main(void) {
int grades[GRADES_LENGTH] = {75, 83, 99, 64, 72}; // using CSV to define array
int sum = 0;
for (int i = 0; i < GRADES_LENGTH; ++i) {
sum += grades[i];
}
int average = sum / GRADES_LENGTH;
printf("Average: %d\n", average);
return 0;
}
Checking the Byte Size of an Array
Arrays will take up the maximum amount of bytes in memory required for all the elements in their array.
- For instance, if a
boolarray of size 3 is initialized, it will take upbytes in memory.
To check the byte size of an array, we can use thesizeof()operator. We can use this to check the number of elements in an array as well, by simply dividing the total size of the array by the size of an element, since each element in an array is the same type and thus the same byte size.
Example: Checking the length of an array using sizeof()
#include <stdio.h>
int main(void) {
int array[3];
printf("%ld\n", (sizeof(array)/sizeof(array[0])));
return 0;
}
Creating Arrays of Undefined Length
We can also forego the setting of the array length assignment altogether, by using:
<type> name[] = {<csv>}
This allows you to dynamically add elements to the array, without having to define a maximum size.
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_LENGTH(arr) (sizeof(arr) / sizeof((arr)[0]))
// defining array length to be the byte size of the whole array, divided by the byte size of the first element.
// it is important to do this operation because sizeof() returns the byte size, and the byte size of elements in an array can differ depending on the type of value stored.
int main(void) {
int grades[] = {75, 83, 99, 64, 72};
int gradesLength = ARRAY_LENGTH(grades);
int sum = 0;
for (int i = 0; i < gradesLength; ++i) {
sum += grades[i];
}
int average = sum / gradesLength;
printf("Average: %d\n", average);
return EXIT_SUCCESS;
}
I don't think they'll ask this in any assessments, but if you haven't declared the value of an element in an array, it is supposed to default to being 0; however, I tried this and it seemed to output memory addresses for some elements, for some reason? I think that might just be something with my compiler? When I try using the embedded C runner in Obsidian it kinda just errors. ↩︎