In the main function, 1.) write code to declare a static array named static_array with a fixed size of 5 integers. Similarly, 2.) write code to dynamically allocate an array named dynamic_array that will be used to store 5 integers. Finally, initialize the values in both arrays to be [1, 2, 3, 4, 5].
#include <stdio.h>
int main(void) {
//write code to declare a static array named static_array
//write code to dynamically allocate an array named dynamic_array
//initialize the values in both arrays to be [1,2,3,4,5]
return 0;
}
int static_array[5];
int *dynamic_array;
int i;
// allocate heap space for dynamic_array
dynamic_array = (int *) malloc(sizeof(int) * 5);
// Initialize arrays with values 1 to size
for (i = 0; i < 5; i++) {
static_array[i] = i + 1;
dynamic_array[i] = i + 1;
}
Checkpoint2.4.5.Dynamic Memory Allocation (Q4).
Given the following code, which of the calls to do_something are valid? Select all that apply.
void do_something(int *array, int size);
int arr1[10];
int *arr2;
arr2 = malloc(sizeof(int)*10);
// assume some initialization code is here
// call do_something here
//void print_array(int *array, int size) { //is also valid
void print_array(int array[], int size) {
int i;
for (i = 0; i < size; i++) {
printf("%d ", array[i]);
}
}
#include <stdio.h>
#include <stdlib.h>
void foo(int *b, int c, int *arr, int n) ;
void blah(int *r, int s);
int main(void) {
int x, y, *arr;
arr = (int *)malloc(sizeof(int)*5);
if (arr == NULL) {
exit(1); // should print out nice error msg first
}
x = 10;
y = 20;
printf("x = %d y = %d\n", x, y);
foo(&x, y, arr, 5);
printf("x = %d y = %d arr[0] = %d arr[3] = %d\n",
x, y, arr[0],arr[3]);
free(arr);
return 0;
}
void foo(int *b, int c, int *arr, int n) {
int i;
c = 2;
for (i=0; i<n; i++) {
arr[i] = i + c;
}
*arr = 13;
blah(b, c);
}
void blah(int *r, int s) {
*r = 3;
s = 4;
// DRAW STACK HERE
}
(a)
Step through the execution of this program. What is its output?