Making Arrays
Meet the Array(Notation and Similarity to Java)
C vs Java
Array Initialization
Array Initialization
Array Initialization
You can let the compiler determine the size
– by measuring the length of your initialization list
int a[] = { 7, 14, 21, 28, 35 };
* C99 offers an alternative initialization syntax
int a[] = { [7] = 10, [2] = 40, [5] = 35 };
0 0 40 0 0 35 0 10
* You can even mix the two notations
– Where you give an index, C will put the given
value there.
– Where there’s no index, C will fill the subsequent index.
– If you don’t give an array size, you’ll get an array that’s just large enough to hold the last value
int a[] = { 1, [7] = 10, 90, [2] = 40, 2,
[5] = 35 };
1 0 40 2 0 35 0 10 90
Array Bounds Checking
Looking at Memory when Array goes out of Bounds
Array Bounds Checking Java vs C
Buffer Overflow
Operations on Array
Choosing Array Size
The size of a statically allocated array must be
a constant expression.
int sequence[ 100 ];
int main(){
…
* This is true in some other contexts also.
* … and used to be true for stack-allocated
arrays.
Variable-Length Arrays
Array Size in Memory
Variable-Length Array Initilization
Computing Array Size
Allocating Large Arrays
Arrays and Stack Space
Array and Functions
Arrays as Parameters
Array Parameter Size
Working with Strings
Initializing a String
Strings in C