By using pointers, and dynamic memory allocation – we have to declare a character pointer, allocate memory at run time in C language.
Example:
#include <stdio.h> #include <string.h> #define MAX 100 int main() { //declaring character pointer char *buffer; //allocating memory at run time buffer = (char*)malloc(MAX*sizeof(char)); if(buffer==NULL) { printf("Error in allocating memory!!!\n"); return -1; } //assign any string strcpy(buffer,"Hello, World"); //printing printf("buffer: %s", buffer); //freeing memory free(buffer); return 0; }
Output
buffer: Hello, World
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Example:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer