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

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile