我是 c 语言的新手,我正在尝试编写一个使用指针创建数组的程序,
#include <stdio.h>
int main()
{
int *i;
scanf("%d",i);i++;
while(1){
scanf("%d",i);
if(*i==*(i-1)){
break;}
printf("%d\n",*i);
i++;
}
return 0;
}
我不断收到此错误
Command failed: ./a.out Segmentation fault
最佳答案
我认为您想要创建一个数组并向其中读取数据,然后使用指针动态显示它。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *A, n;
printf("\n Enter the size of array : "); // Reading the size of array
scanf("%d",&n);
A = (int*) malloc(sizeof(int) * n); // Allocating memory for array
int i = 0;
for(i = 0; i < n; i++) // Reading data to array
scanf("%d", (A+i));
// Operations on array
for(i = 0; i < n; i++) // Printing array
printf("%d ", A[i]);
return 0;
}
希望这有帮助。!!
关于c - 在c中使用指针创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46503701/