Skip to main content

Details

This section is a summary of arrays and C strings

Array Declaration and Initialization

To declare an array we must supply a capacity indicating how big the array is. The basic syntax is as follows:

Syntax

datatype arrayName[capacity];

Examples

int arrayOfInts[7];  	//array of 7 integers
float arrayOfFloats[3]; //array of 3 floats

image of the two arrays

Initialization

We can individually initialze each element of the array by using a comma separated list of values. Each item in the list will be placed into the corresponding element. If there are too few numbers in the list of values, the remaining element's values will be undefined

int numbers[5] = {5, -2, 1};

Above code creates this:

image of the array

For safety, we also may want to initialize an array to all zeros. We can do this by using 0. This will put a 0 value in every element of the array and not just the first.

int justZeros[8] = {0}

Above code creates this:

image of the array

Bound checking is not automatic!

C does not check array bounds automatically. Accessing elements outside the array can cause serious problems:

int numbers[5] = {1, 2, 3, 4, 5};
numbers[10] = 99; // DANGEROUS!

Accessing outside array bounds...this will not be flagged as an error. It will cause errors that manifest itself in weird ways making it seem like other parts of your program is an error.

C Strings

C-Strings are also known as null-terminated character strings. C-Strings are essentially character arrays that end at a null character (\0). This null char is REQUIRED for C strings to function correctly. Overwriting this null leads to major errors that can cause programs to crash and potentially lead to serious security flaws

Declaration:

Since C-Strings are just null terminated character arrays, declaring a C-String is no different than declaring a character array. However, we must make sure that the array is big enough to hold our content AND an additional element just for the null character. For example suppose we were given these specs:

Create a string that can hold text of up to 12 characters.

Our array declaration would need to be as follows:

char myString[13];

Initialization

When you declare a string you can initialize it using a string literal which is delimited using double quotes (")

char greeting[20] = "Hello";
char name[10] = "Austin";

The above creates strings that look like:

image of 2 arrays containing the words "Hello" and "Austin", both strings end with a null character

Notice that the null character \0 is automatically added at the end of the string literal.

You can also initialize a string using the {} notation with individual characters:

char word[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

When using this method, you must remember to include the null character \0 yourself.

You can also initialize a string to be empty by setting all elements to zero:

char emptyString[50] = {0};

This creates a string where the first character is \0, making it an empty string.

String Literal Assignment Limitation

String literals can only be used during initialization. You cannot assign a string literal to an existing string variable:

char message[20] = "Hello";  // ✓ Valid - initialization
message = "Goodbye"; // ✗ Invalid - assignment after creation

Once a string is created, you must modify it character by character or use string manipulation functions.

Patterns

Iterating Through an Array

When working with arrays, you typically need to know how many elements are actually being used. Here's the standard pattern for iterating through an array:

int i;
int numbers[10] = {5, 3, 8, 1, 9};
int used = 5; // number of elements actually stored

// Pattern: iterate through used elements
for (i = 0; i < used; i++) {
printf("%d ", numbers[i]);
}

Iterating Through a String

For strings, you iterate until you reach the null character \0:

int i;
char word[] = "Hello";

// Pattern: iterate until null character
for (i = 0; word[i] != '\0'; i++) {
printf("%c ", word[i]);
}