Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Declaring and Initializing Arrays | Introduction to Arrays
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Arrays

bookDeclaring and Initializing Arrays

When you work with arrays in C++, you need to know how to declare them and set their initial values. The basic syntax for declaring an array includes the type of elements it will hold, the array's name, and its size in square brackets. For example, to declare an array of five integers, you write int numbers[5];. You can also initialize arrays in different ways: by leaving them uninitialized, by setting all elements to zero, or by providing explicit values for each element.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#include <iostream> int main() { // Uninitialized array int uninitialized[5]; // Zero-initialized array int zeroed[5] = {}; // Explicitly initialized array int explicitVals[5] = {1, 2, 3, 4, 5}; std::cout << "Uninitialized: "; for (int i = 0; i < 5; ++i) std::cout << uninitialized[i] << " "; std::cout << "\nZero-initialized: "; for (int i = 0; i < 5; ++i) std::cout << zeroed[i] << " "; std::cout << "\nExplicit values: "; for (int i = 0; i < 5; ++i) std::cout << explicitVals[i] << " "; std::cout << std::endl; }

In the code above, you see three different ways to declare and initialize an integer array. The first array, uninitialized, is declared with a size of 5 but without any initial values. This means its contents are undefined and could be anything. The second array, zeroed, is declared with empty braces, which sets all its elements to zero. The third array, explicitVals, is initialized with specific values for each element, so you know exactly what is stored in each position. The output will show you the differences: zeroed will always be all zeros, explicitVals will always match the specified numbers, but uninitialized may display unpredictable values.

main.cpp

main.cpp

copy
1234567891011
#include <iostream> int main() { int arr[4]; // uninitialized array std::cout << "Contents of uninitialized array: "; for (int i = 0; i < 4; ++i) std::cout << arr[i] << " "; std::cout << std::endl; }

If you forget to initialize an array, it may contain whatever data was previously in that area of memory, resulting in garbage values. This can cause your program to behave unpredictably, making it hard to find and fix bugs. Always initialize your arrays to avoid these issues.

Note
Note

Uninitialized arrays may contain garbage values, leading to bugs.

question mark

What is the result of declaring an int array without initialization in C++?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you show me examples of how to initialize arrays with different data types?

What happens if I try to access an array element outside its bounds?

Can you explain more about the risks of using uninitialized arrays?

bookDeclaring and Initializing Arrays

Svep för att visa menyn

When you work with arrays in C++, you need to know how to declare them and set their initial values. The basic syntax for declaring an array includes the type of elements it will hold, the array's name, and its size in square brackets. For example, to declare an array of five integers, you write int numbers[5];. You can also initialize arrays in different ways: by leaving them uninitialized, by setting all elements to zero, or by providing explicit values for each element.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#include <iostream> int main() { // Uninitialized array int uninitialized[5]; // Zero-initialized array int zeroed[5] = {}; // Explicitly initialized array int explicitVals[5] = {1, 2, 3, 4, 5}; std::cout << "Uninitialized: "; for (int i = 0; i < 5; ++i) std::cout << uninitialized[i] << " "; std::cout << "\nZero-initialized: "; for (int i = 0; i < 5; ++i) std::cout << zeroed[i] << " "; std::cout << "\nExplicit values: "; for (int i = 0; i < 5; ++i) std::cout << explicitVals[i] << " "; std::cout << std::endl; }

In the code above, you see three different ways to declare and initialize an integer array. The first array, uninitialized, is declared with a size of 5 but without any initial values. This means its contents are undefined and could be anything. The second array, zeroed, is declared with empty braces, which sets all its elements to zero. The third array, explicitVals, is initialized with specific values for each element, so you know exactly what is stored in each position. The output will show you the differences: zeroed will always be all zeros, explicitVals will always match the specified numbers, but uninitialized may display unpredictable values.

main.cpp

main.cpp

copy
1234567891011
#include <iostream> int main() { int arr[4]; // uninitialized array std::cout << "Contents of uninitialized array: "; for (int i = 0; i < 4; ++i) std::cout << arr[i] << " "; std::cout << std::endl; }

If you forget to initialize an array, it may contain whatever data was previously in that area of memory, resulting in garbage values. This can cause your program to behave unpredictably, making it hard to find and fix bugs. Always initialize your arrays to avoid these issues.

Note
Note

Uninitialized arrays may contain garbage values, leading to bugs.

question mark

What is the result of declaring an int array without initialization in C++?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 2
some-alt