Hello and welcome.
In this lecture, we're going to talk about variable length arrays.
What exactly does this mean?
Well, this is a new feature that was added I think for C99.
But up to this point, all we know about arrays, besides being
multi-dimensions and also besides being a cool feature where you
can store multiple data elements in a single variable essentially,
is that they're fixed size.
Once you specify that size, you have to use a hard-coded constant.
When you declare the variable that specifies the size
and it stays the same way.
Variable length arrays is a bit misleading.
It doesn't mean you're going to change the size of the array dynamically.
It's still going to be the same size, but now you can use variables.
So the term variable in variable length array does not mean that
you can modify the length of the array after you create it.
What it does mean is that you now can set a variable to use for that array.
so a VLA is still going to keep the same size after creation.
The variable array allows you to specify the size and array with a
variable when creating an array.
So you can just use variables, that's all it really means.
It seems like it might not be that useful.
But actually the only reason it was really added was because
it helped with really existing libraries and other languages.
So C99 introduced this new feature and it is used and that's
why I wanted to mention it.
I’m only going to mention it briefly, I’m not going to provide
a detailed use case of it.
I just wanted to tell you that exists because sometimes you see this term
variable length arrays quite a bit when you're reading up about c.
And I wouldn't want you to see it and say what the heck does
this mean when we studied arrays and we didn't mention the topic.
So it was introduced mainly to make c a better language
for numerical computing.
VLA is which variable length arrays make it a lot easier to convert
existing libraries of Fortran numerical calculation routines to c.
So this is why it was added.
So if you're a Fortran programmer, you may want to
use this when you're migrating or if you're converting code.
If you're porting your Fortran code trying to convert to
c, you may want to use this.
You cannot initialize a VLA in its declaration.
So there's no initialization when you're talking about
variable length arrays.
Here are some valid sizes that you can apply when declaring an array.
So we can declare two variables and an m.
And then you see at the bottom when we say float 8 and float a9,
we can use those variables when we declare them in the array size.
And that was not allowed before C99 but now it is and that's
VLA, variable length array.
Specifying a variable to be used inside of an array.
It's not changing the size at runtime.
There's other examples here valid declarations of arrays as well that
you may want to pay attention to.
Perfectly fine if you're setting an array to a specific size, like 5.
You can use expressions when you assign the size of an array.
So in the next example 5 times 2 plus 1, that's fine.
The other thing to notice here is we're using the sizeof operator, and
that's perfectly fine too because that's going to return an int.
Sizeof int is going to either return 4 or 8 depending on the machine
you're running on and then you're adding 1 to it, perfectly fine.
Putting a negative number inside the declaration of
an array, you can't do that.
There's no such thing as a negative 4.
The size of the array has to be greater than 0.
So also zero will not work.
2.5 putting a float, can't do that either, it doesn't make sense.
How can you create an array of size 2.5.
However, if you do put 2.5 inside the square brackets and you
cast it to an int, that's okay.
Again didn't want to spend a lot of time on variable length arrays, I
just want to introduce the concept.
If you hear the term, you know what it is.
Assigning variables to an array when declaring them for the size.
Really just introduced for compatibility with
existing Fortran libraries. Thank you.