Using C programming
Write a code snippet using calloc() to allocate memory for astring of 200 characters. make sure you include the explicit castto a pointer of type char. You must declare all variables andarrays correctly.
Solution
Thanks for the question, here is the way to call calloc()function and cast it to char pointer
Relevant comments provided for all important lines.
===================================================================================
#include<stdio.h>
#include<stdlib.h>
int main() {
// pointer to the char array
char *cPtr;
//include the explicit cast to a pointer of type char
//for a string of 200 characters
cPtr = (char*)calloc(200,sizeof(char));
if(cPtr == NULL) {
printf(“Error! memory not allocated.”);
exit(0);
} else {
printf(“Memory allocated.”);
}
}
=======================================================================================
If you run the program, it should say that