answer is: B. Please explain why the answer is what is is thoroughly. I am tryin
ID: 3827631 • Letter: A
Question
answer is: B.
Please explain why the answer is what is is thoroughly. I am trying to study for my test. I will make sure to thumbs up! thank you very much for your time
1. n the following program int main char *p1; statement 1 return 0; Which of the following statements in place of statement1 will not cause a segmentation fault? a. printf s A pl) b. pl hello pl d. None of the above Answer 2. Which of the following correctly declares a dynamic array of integers? a) int a; a ma lloc (sizeof (int) b) int a a malloc (n sizeof (int) c) int *a, i a malloc (n* sizeof (int) d) int *a, a n malloc (size of (int)) AnswerExplanation / Answer
================================================
Q1.
----------
Answer:
----------
b. p1 = "hello";
----------
Explanation:
----------
#include<stdio.h>
int main()
{
char *p1;
p1="hello";
return 0;
}
This block of statement is valid code, since we are assigning array of characters
to p1 char array which is correct.
When we look other options a and c , option a is wrong since pointer can not be declared
in print statement. When comes to option c , For pointer we can not assign direclty single char.
================================================
Q2.
----------
Answer:
----------
c. int *a;
a = malloc(n*sizeof(int));
----------
Explanation:
----------
#include<stdio.h>
int main()
{
int *a,n=10;
a = malloc(n*sizeof(int));
return 0;
}
This block of staemnt is valid code, since we are assinging memory for entier array. i.e total number of elements multiply by size of int.
When comes to other options a,b integer declarion itself wrong. For option d , memory can not multiply by external identified.
Hence option c only correct with proper systax.
================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.