CSC 253 Lecture 12 Compiletime vs runtime arrays








- Slides: 8
CSC 253 Lecture 12
Compile-time vs. run-time arrays w What are some advantages of compile -time arrays? a. b. c. d. e. f. Save memory Can be of variable size Shorter code Both a and b Both a and c Both b and c
Compile-time vs. run-time arrays w We ended Lecture 9 with a program that would print out the contents of a file (lecture_09/examples/3. c). w Let’s take the code for printing and make it into a function print_arr. w Parameter 1: Name of array w Parameter 2: Length of array w Parameter 3: Message (e. g. , array name) w We can then use it to print out the contents of compile-time and run-time arrays.
Other array types w What should we change in this program to handle a character array instead of an integer array? a. b. c. d. e. f. g. The declaration of the array One or more data types in print_arr The field width for printing Both a and b Both a and c Both b and c All of a, b, and c
Let’s look at an array of chars w I changed the printing function to … w Print out the address of each character w Print out the character in hex and character format w In hex format, only two digits need to be printed.
Two-dimensional arrays w First, let’s declare a compile-time twodimensional array and print it out. w Then, let’s allocate the same array at run time and print it out.
When we print out a 2 D array, what do we expect to see? a. The same amount of space between rows in each array. b. More space between rows in the compile-time allocated array. c. More space between rows in the run -time allocated array.
Two-dimensional arrays w When passing a two-dimensional array as a parameter, the second dimension must be given as a constant because a. The rows are allocated at compile time. b. The elements of a single column are stored adjacent to each other. c. The compiler needs to know how long the first row is to find the beginning of the second row.