<分区>
基本上,我试图编写一个简单的 C 函数,提示用户输入数组长度,然后要求用户输入数组的值(整数)。
所需的示例输出:
Enter Array Length: 5
Enter values for the array:
1 2 3 6 7
The current array is:
1 2 3 6 7
这是我目前的代码。我觉得这应该行得通,但由于对 C 语言有如此基本的了解,它会导致段错误。
int intersect()
{
int size, index, input;
printf("Enter the size of the arrays:\n");
scanf("%d", &size);
int arr1[size], arr2[size];
index = 0;
printf("Enter the elements of the first array:\n");
while (index < sizeof(arr1))
{
scanf("%d ", &input);
arr1[index] = input;
index = index + 1;
}
printf("The current array is:\n %d", arr1);
}
我不明白如何为用户定义长度的数组收集输入。任何解释表示赞赏!