Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Introduction to Arrays | Data
C Basics

bookIntroduction to Arrays

Sometimes you need to create hundreds or even thousands of variables. Creating them one by one isn't practical. That's where arrays come in. An array is a collection of variables of the same type. If a single variable is like one storage box, an array is a warehouse filled with boxes, each holding its own value. Declaring an array looks something like this:

int array[3];

Here's how you declare an array with space for three elements. To assign values to it, use curly braces to list them inside.

int array[3] = {1, 5, 10};
int array[] = {56, 3, 10};
Note
Note

If you specify the items directly, you don't need to declare the size, the compiler automatically counts and assigns the number of elements.

Indexes

Each box in an array has its own unique identifier, called an index, which lets you easily access specific elements. An index is the number assigned to each item in the array, similar to your position in line at a coffee shop. Indexes in C start at zero, meaning the first element has an index of 0.

Main.c

Main.c

copy
12345678910
#include <stdio.h> int main() { // Array declaration int array[3] = {56, 3, 10}; // Display the first element of the array printf("%d", array[0]); return 0; }
Note
Note

The arrays discussed so far are static, meaning their size stays fixed during the program’s execution. There are also dynamic arrays, which can change size while the program is running.

You can change the value of any element in an array by referring to its specific index.

main.c

main.c

copy
123456789101112131415161718
#include <stdio.h> int main() { int array[3] = { 56, 3, 10 }; printf("%d ", array[0]); printf("%d ", array[1]); printf("%d\n", array[2]); array[1] = 555; // change 10 to 555 printf("%d ", array[0]); printf("%d ", array[1]); printf("%d\n", array[2]); return 0; }
question mark

Given the following array declaration in C, what value will be printed by the code below?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you show me how to access a specific element in an array?

How do I change the value of an element in an array?

What happens if I try to access an index that doesn't exist?

Awesome!

Completion rate improved to 2.63

bookIntroduction to Arrays

Sveip for å vise menyen

Sometimes you need to create hundreds or even thousands of variables. Creating them one by one isn't practical. That's where arrays come in. An array is a collection of variables of the same type. If a single variable is like one storage box, an array is a warehouse filled with boxes, each holding its own value. Declaring an array looks something like this:

int array[3];

Here's how you declare an array with space for three elements. To assign values to it, use curly braces to list them inside.

int array[3] = {1, 5, 10};
int array[] = {56, 3, 10};
Note
Note

If you specify the items directly, you don't need to declare the size, the compiler automatically counts and assigns the number of elements.

Indexes

Each box in an array has its own unique identifier, called an index, which lets you easily access specific elements. An index is the number assigned to each item in the array, similar to your position in line at a coffee shop. Indexes in C start at zero, meaning the first element has an index of 0.

Main.c

Main.c

copy
12345678910
#include <stdio.h> int main() { // Array declaration int array[3] = {56, 3, 10}; // Display the first element of the array printf("%d", array[0]); return 0; }
Note
Note

The arrays discussed so far are static, meaning their size stays fixed during the program’s execution. There are also dynamic arrays, which can change size while the program is running.

You can change the value of any element in an array by referring to its specific index.

main.c

main.c

copy
123456789101112131415161718
#include <stdio.h> int main() { int array[3] = { 56, 3, 10 }; printf("%d ", array[0]); printf("%d ", array[1]); printf("%d\n", array[2]); array[1] = 555; // change 10 to 555 printf("%d ", array[0]); printf("%d ", array[1]); printf("%d\n", array[2]); return 0; }
question mark

Given the following array declaration in C, what value will be printed by the code below?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
some-alt