C语言用用户定义的大小分配和初始化一个数组
示例
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int * pdata;
size_t n;
printf ("输入数组的大小: ");
fflush(stdout); /* Make sure the prompt gets printed to buffered stdout. */
if (1 != scanf("%zu", &n)) /* If zu is not supported (Windows?) use lu. */
{
fprintf("scanf() did not read a in proper value.\n");
exit(EXIT_FAILURE);
}
pdata = calloc(n, sizeof *pdata);
if (NULL == pdata)
{
perror("calloc() failed"); /* Print error. */
exit(EXIT_FAILURE);
}
free(pdata); /* Clean up. */
return EXIT_SUCCESS;
}该程序尝试扫描标准输入中的无符号整数值,并通过调用函数n为类型为元素的数组分配一块内存。后者将存储器初始化为全零。intcalloc()
如果成功,则通过调用释放内存free()。
热门推荐
10 简短元宵节祝福语
11 虎年贺卡和祝福语简短
12 生日奥特曼可爱祝福语简短
13 商务感恩祝福语大全简短
14 校庆祝福语简短独特
15 幼师离别祝福语老师简短
16 火把节祝福语简短
17 对新人祝福语大全简短
18 简短新年红包祝福语大全