Class 13: Arrays and Pointers Flashcards
Statically allocated arrays
Compiler generates code to allocate the space for the array elements at compile time. The space is allocated on the stack or the heap depending on the declaration statement. (File or block scope/ static or automatic class)
Dynamically allocate arrays
A c library function is called to request space for the array elements at runtime. This is after the program starts running. Need to declare a pointer to array we want space to reprint to hold the address of this block of memory
What arrays are these
Int scores[6] = {19,17,18,16,15,20};
Int scores[] = {19,17,18,16,15,20}
Statically allocated arrays
Arrays of static storage class will be initialized to ____
0
Arrays of ___ storage class are not initialized in any way unless specifically done so with code
Automatic
Is there a library function for copying arrays in c?
NO
Int scores[3] = {1,2,3};
Scores is equivalent o
&scores[0]
In c, the name of the array itself is a _____ to the _____ element of the array
A constant pointer to the first element of the array
Because in arrays, pointer to the first element of an array is CONSTANT, it _____ be changed. You CANNOT assigne a different address to it
Cannot
Int scores[3] = {1,2,3};
Int scores2[2] = {4,5};
Int *scores_ptr;
Scores_ptr = scores2;
Scores = scores_ptr ——> INCORRECT
All array elements are stored in _____ memory locations
Contiguous
What is *(scores + 3) equivalent to?
Scores + 3
Scores + (3sizeof(int))
&scores[0] + (3sizeof(int))
Opening a file
FILE *input_file;
Input_file = fopen(“lab2_datafile”,”r”);
Or
Char filename[256];
Scanf(“%s”,filename);
Input_file = fopen(filename,”r”);
How to read front file
FILE *output_file;
Output_file = fopen(“lab2_output”, “w”);
Fscanf(file_name, “%f”, &float_val);
How to write to a file
Fprintf(file_name, …)
How to close a file
Fclose(file_name)