-4
我在使用malloc函数的c中创建了一个程序。 代码:c中的Malloc函数
#include<stdio.h>
#include<stdbool.h>
#include<malloc.h>
int main(){
int n;
int *ptr,i,sum;
sum = 0;
printf("Enter the number = ");
scanf("%d",&n);
ptr = (int *)(malloc(10));
for(i=0;i<n;i++){
scanf("%d",ptr+i);
sum += *(ptr+i);
}
printf("The sum of the numbers is = %i",sum);
}
我已经使用malloc函数。如何是有可能,10点的整数被存储在10个字节到分配的10个字节的存储器....
整数通常需要4个字节,并且您正在写出界限 – Pooya
*“10个整数存储在10个字节中怎么可能......”*不是。 –
您必须添加数据类型的大小:'ptr = malloc(sizeof(int)* n);' – jboockmann