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
How to strikethrough text in Word, write strikethrough text in Word and Excel
Instructions for using the TRIMRANGE function to clean up Excel tables
Instructions for saving images from Powerpoint to your computer
Instructions for inserting square root symbol in Powerpoint
How to use abstract images to create phone wallpapers
10 strangest military vehicles in world history