程序填空题:使用动态内存分配求整数和
输入一个正整数$$n$$($$1
```c++
# include
# include
int main()
{
int i, n, sum;
int *p;
scanf("%d", &n);
if (@@[(p = (int *)calloc(n, sizeof(int))) == NULL](1)) {
printf("Not able to allocate memory. \n");
exit(1);
}
for (i = 0; i < n; i++){
@@[scanf("%d", p+i)](1);
}
sum = 0;
for (i = 0; i < n; i++) {
sum = sum +@@[ *(p+i)](1);
}
printf("%d\n",sum);
free(p) ;
return 0;
}
```
答案:
第1空:(p = (int *)calloc(n, sizeof(int))) == NULL
第2空:scanf("%d", p+i)
第3空: *(p+i)
```c++
# include
# include
int main()
{
int i, n, sum;
int *p;
scanf("%d", &n);
if (@@[(p = (int *)calloc(n, sizeof(int))) == NULL](1)) {
printf("Not able to allocate memory. \n");
exit(1);
}
for (i = 0; i < n; i++){
@@[scanf("%d", p+i)](1);
}
sum = 0;
for (i = 0; i < n; i++) {
sum = sum +@@[ *(p+i)](1);
}
printf("%d\n",sum);
free(p) ;
return 0;
}
```
答案:
第1空:(p = (int *)calloc(n, sizeof(int))) == NULL
第2空:scanf("%d", p+i)
第3空: *(p+i)