YouTip LogoYouTip

C Function Malloc

# C Library Function - malloc() [![Image 3: C Standard Library - ](#) C Standard Library - ](#) ## Description The C library function **void *malloc(size_t size)** allocates the requested memory and returns a pointer to it. ## Declaration Below is the declaration for the malloc() function. void *malloc(size_t size) ## Parameters * **size** -- This is the size of the memory block, in bytes. ## Return Value This function returns a pointer to the allocated memory, or NULL if the request fails. ## Example The following example demonstrates the usage of the malloc() function. ## Example #include#include#includeint main(){char *str; /* Initial memory allocation */str = (char *)malloc(15); strcpy(str, "tutorial"); printf("String = %s, Address = %un", str, str); /* Reallocating memory */str = (char *)realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %un", str, str); free(str); return(0); } Let us compile and run the above program, this will produce the following result: String = tutorial, Address = 3662685808String = , Address = 3662685808 [![Image 4: C Standard Library - ](#) C Standard Library - ](#)
← Cpp Examples LcmCpp Examples Pyramid Pattern β†’