亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

C 語言基礎教程

C 語言流程控制

C 語言函數(shù)

C 語言數(shù)組

C 語言指針

C 語言字符串

C 語言結構體

C 語言文件

C 其他

C 語言參考手冊

C 庫函數(shù) malloc() 使用方法及示例

C 標準庫 - <stdlib.h>

C 庫函數(shù) void *malloc(size_t size) 分配所需的內存空間,并返回一個指向它的指針。

聲明

下面是 malloc() 函數(shù)的聲明。

void *malloc(size_t size)

參數(shù)

  • size -- 內存塊的大小,以字節(jié)為單位。

返回值

該函數(shù)返回一個指針 ,指向已分配大小的內存。如果請求失敗,則返回 NULL。

在線示例

下面的示例演示了 malloc() 函數(shù)的用法。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main()
{
   char *str;
 
   /* 最初的內存分配 */
   str = (char *) malloc(15);
   strcpy(str, "nhooo");
   printf("String = %s,  Address = %u\n", str, str);
 
   /* 重新分配內存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);
 
   free(str);
 
   return(0);
}

讓我們編譯并運行上面的程序,這將產生以下結果:

String = nhooo,  Address = 3662685808
String = (cainiaoplus.com),  Address = 3662685808

C 標準庫 - <stdlib.h>