程序应该要求用户输入数组的长度和单元格的值。
为什么用户需要比我在条件中写的多输入1个数字?
int main()
{
int length, i, *p;
printf("n Please enter the length of the array: n");
scanf("%d", &length);
p = (int*)malloc(length*sizeof(int));
printf("n Please enter %d values: n", length);
for(i=0; i<length; i++){
scanf("n %d n", &p[i]);
if(i==0){
*(p+p[i]) = p[i];
}else{
*(p+p[i]) += p[i];
}
}
您需要更改第二条扫描线。
scanf("n %d n", &p[i]); /* Replace this line */
scanf("n %d", &p[i]); /* By this one */