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>];

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>


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>}

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.

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;
}


  1. 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. ↩︎