Log in from a computer to take this course

You'll need to log in from a computer to start Learn C. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
Learn

Array elements can be accessed, modified, and used just like any other variable of the same data type. The following shows how to access an element in an array at index idx:

arr[idx]

The first element in an array has index 0 and the last element in an array has index arraySize - 1. The nth element is at index n-1, so, for example, the third element would be at index 2.

An element in an array is modified just like a regular variable:

arr[idx] = newValue

Remember to be consistent with the data types! For example, elements of an array of integers can only be modified to represent other integer values; anything else will cause errors. When working with arrays, it is important to be careful and not attempt to access an element at an index greater than arraySize - 1 as the memory located there could be storing other data. Accessing an array at an index greater than arraySize - 1 will return a random value. Modifying an element at an index greater than arraySize - 1 will corrupt the data stored at that location, causing the program to behave in an unpredictable manner. The same is true for array access at indices less than zero.

The following piece of code changes an element in an array and assigns it to a lone variable:

#include<stdio.h> int main() { int arr[] = {3, 5, 7, 9}; // Array creation arr[2] = 6; // Modify the third element int x = arr[2]; // Assign the third element to the lone variable x printf("%i", x); // Print x }

Instructions

1.

The accompanying code erroneously attempts to print the last value in the evens array. Fix this bug.

2.

Print the third element of the evens array.

3.

The odds array given is intended to contain the first six odd numbers. Notice that some values are incorrect. Fix them by changing their value to the correct odd number. Do this by using array access.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?