Using C Programming Write Code Snippet Using Calloc Allocate Memory String 200 Characters Q37132608

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

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.