Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given the following structure: struct Data { int n; char* name; int* arr[3]; }; n 33 name ? 7 0 0 0 0 arr -
Given the following structure: struct Data { int n; char* name; int* arr[3]; }; n 33 name ? 7 0 0 0 0 arr - 0 0 0 0 0 0 0 0 Which of the following code fragments dynamically allocate the memory as shown in the diagram above? 1) struct Data *sptr = malloc(sizeof(struct Data)); if (sptr == NULL) { printf("Unable to allocate memory. "); exit(1); } sptr->n = 33; sptr->name = malloc(12 * sizeof(char)); for (int i = 0; i arr[i] = malloc(4 * sizeof(int)); if (sptr->arr[i] == NULL) { printf("Unable to allocate memory. "); exit(1); } for (int j = 0; j arr[i] + j) = 0; 2) struct Data *sptr = malloc(sizeof(struct Data)); if (sptr == NULL) { printf("Unable to allocate memory. "); exit(1); } sptr->n = 33; sptr->name = malloc(12 * sizeof(char)); if (sptr->name == NULL) { printf("Unable to allocate memory. "); exit(1); } for (int i = 0; i arr[i] = malloc(4 * sizeof(int)); if (sptr->arr[i] == NULL) { printf("Unable to allocate memory. "); exit(1); } for (int j = 0; j arr[i] + i) = 0; } 3) struct Data *sptr = malloc(sizeof(struct Data)); if (sptr == NULL) { printf("Unable to allocate memory. "); exit(1); } sptr->n = 33; sptr->name = malloc(12 * sizeof(char)); if (sptr->name == NULL) { printf("Unable to allocate memory. "); exit(1); } for (int i = 0; i arr[i] = malloc(2 * sizeof(int)); if (sptr->arr[i] == NULL) { printf("Unable to allocate memory. "); exit(1); } for (int j = 0; j arr[i] + 1) = 0; } for (int i = 0; i arr[i] = realloc(4 * sizeof(int)); if (sptr->arr[i] == NULL) { printf("Unable to allocate memory. In"); exit(1); } } 0 1 and 2 0 2 and 3 O 2 only O 3 only O1 only
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started