Malloc () function in C
The malloc () function allocates the requested memory and returns a pointer to it.
The malloc () function allocates the requested memory and returns a pointer to it.
Declare malloc () in C
Below is the declaration for malloc () in C:
void * malloc ( kich - co )
Parameters
kich-co : This is the size of the memory block (in bytes).
Returns the value
This function returns a pointer to the allocated memory, or returns NULL if the request fails.
For example
The following C program illustrates the usage of malloc () in C:
#include #include #include int main () { char * str ; /* lan cap phat bo nho ban bau */ str = ( char *) malloc ( 15 ); strcpy ( str , "QTMTeam" ); printf ( "Chuoi = %s, tai Dia chi = %un" , str , str ); /* tai cap phat lai bo nho */ str = ( char *) realloc ( str , 25 ); strcat ( str , ".@gmail.com" ); printf ( "Chuoi = %s, tai Dia chi = %un" , str , str ); free ( str ); return ( 0 ); }
Compile and run the above C program to see the results.
According to Tutorialspoint
Previous lesson: Free () function in C
Next lesson: Function realloc () in C
5 ★ | 2 Vote
You should read it
Maybe you are interested
Traveling with Kids: How to Make Your Trip Fun and Stress-Free
Instructions for recording live video on ChatGPT Voice
One Piece: Strength Ranking of Straw Hat Crew Members
Instructions for inserting stickers into photos on iPhone
FBI Warns iMessage Is Not a Strongly Encrypted Messaging App
Google Labs Releases Whisk: A Tool That Allows You to Upload Images as Instructions Instead of Text Prompts