Usefullness of arrays

Hey guys,

As I'm yet a real beginner in c++ I was reading through the tutorial on this site. I got to the chapter about arrays and I'm finally getting what everyone is talking about when they mention arrays. Eventhough I don't think I understand it completely, I do get the concept.

Now my question is: what is the usefullness of arrays?

If I've understood it correctly the tutorial states that you can only use arrays for constant values. So that would make arrays less usefull right? (for the basic programs I've made I barely used any constants)

And is i just the basic programming I do right now, or are constants not used alot? (I mean not so much as variables)

Thanks alot for explaining!

-Kaduuk
Stack allocated arrays must have a constant value as their size. Their elements are not required to be constant.
oh ok.
so you could have an array of the size of 5 elements, where the elements are variables (and even mixed with constants)?

e.g. int RandomArray[5] = {a, c, n, 5, k} where a, c, n and k are variables.

And do these variables then also have to be of type int, or can they also be doubles, floats etc?
Every element must have the same type as the array. Example:

1
2
3
4
5
6
7
8
int a = 10;
int arr[3] = { a, 100, 1000.3 };

std::cout << arr[0] << ' ' << arr[1] << ' ' << arr[2] << std::endl;
a = 3;
std::cout << arr[0] << ' ' << arr[1] << ' ' << arr[2] << std::endl;
arr[0] = 5;
std::cout << arr[0] << ' ' << arr[1] << ' ' << arr[2] << std::endl;

This should output:

10 100 1000
10 100 1000
5 100 1000

So, since the type of the array is int (not const int), every element is an int. They're just contiguous in memory.
Last edited on
ok, thank you!
I think I get it :)
Topic archived. No new replies allowed.