你的代码有问题。#include <stdio.h>
#include <stdlib.h> //需要这个头文件
int main(void)
{
int i, large = 0;
int *pnum = (int *)malloc(sizeof(int)); // realloc 需要先有 malloc
long int sum = 0;
for (i = 0; i <= 121; i++)
{
if ((i % 5 == 0) || (i % 3 == 0))
{
large = large + 1;
pnum = (int *)realloc(pnum, large * sizeof(int)); //要说明新开的空间要放什么数据类型
pnum[large - 1] = i;
}
}
printf("121以下能被3或5整除的数有:\n");
for (i = 0; i < large; i++)
{
printf("%d ", pnum[i]);
sum = sum + pnum[i];
}
free(pnum); //记得释放
putchar('\n');
printf("他们的和为%d\n", sum);
return 0;
}
你的代码会动态调整,不怕 |