#include <stdlib.h>
void* malloc (size_t size);
char *ptr = (char *)malloc(10); // 分配10个字节的内存空间,用来存放字符
#include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free, rand, system */
int main ()
{
int i,n;
char * buffer;
printf ("输入字符串的长度:");
scanf ("%d", &i);
buffer = (char*)malloc(i+1); // 字符串最后包含 \0
if(buffer==NULL) exit(1); // 判断是否分配成功
// 随机生成字符串
for(n=0; n<i; n++)
buffer[n] = rand()%26+'a';
buffer[i]='\0';
printf ("随机生成的字符串为:%s\n",buffer);
free(buffer); // 释放内存空间
system("pause");
return 0;
}
输入字符串的长度:20 随机生成的字符串为:phqghumeaylnlfdxfirc
#include <stdlib.h>
void* calloc (size_t num, size_t size);
char *ptr = (char *)calloc(10, 10); // 分配100个字节的内存空间
// calloc() 分配内存空间并初始化 char *str1 = (char *)calloc(10, 2); // malloc() 分配内存空间并用 memset() 初始化 char *str2 = (char *)malloc(20); memset(str2, 0, 20);
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i,n;
int * pData;
printf ("要输入的数字的数目:");
scanf ("%d",&i);
pData = (int*) calloc (i,sizeof(int));
if (pData==NULL) exit (1);
for (n=0;n<i;n++)
{
printf ("请输入数字 #%d:",n+1);
scanf ("%d",&pData[n]);
}
printf ("你输入的数字为:");
for (n=0;n<i;n++) printf ("%d ",pData[n]);
free (pData);
system("pause");
return 0;
}
要输入的数字的数目:4 请输入数字 #1:126 请输入数字 #2:343 请输入数字 #3:45 请输入数字 #4:234 你输入的数字为:126 343 45 234
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有