C stdlib calloc() 函式
示例
分配記憶體並寫入一些值
// Allocate memory for a number of items
int numItems = 15;
int *myArray = calloc(numItems, sizeof(int));
// Write into the memory
for(int i = 0; i < numItems; i++) {
myArray[i] = i + 1;
}
// Display the contents of the memory
for(int i = 0; i < numItems; i++) {
printf("%d ", myArray[i]);
}
// Free the memory
free(myArray);
myArray = NULL;
自己動手試一試 »
定義和用法
calloc()
函式分配記憶體,用零填充它,並返回指向它的指標。
calloc()
函式定義在 <stdlib.h>
標頭檔案中。
要了解更多關於記憶體分配的資訊,請參閱我們的 C 記憶體管理教程。
語法
calloc(size_t amount, size_t size);
size_t
資料型別是一個非負整數。
引數值
引數 | 描述 |
---|---|
amount | 指定要為其分配記憶體的項的數量。 |
大小 | 指定每個項的大小(以位元組為單位)。 |
技術詳情
返回 | 指向新分配記憶體塊的 void * 指標。 |
---|