C Programming - Memory Allocation
Exercise : Memory Allocation - Point Out Errors
1.
Point out the error in the following program.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a[3];
a = (int*) malloc(sizeof(int)*3);
free(a);
return 0;
}
Answer: Option
Explanation:
We should store the address in a[i]
2.
Point out the error in the following program.
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *ptr;
*ptr = (char)malloc(30);
strcpy(ptr, "RAM");
printf("%s", ptr);
free(ptr);
return 0;
}
Answer: Option
Explanation:
Answer: ptr = (char*)malloc(30);
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers