기본적으로 사용자에게 배열 길이를 묻는 간단한 C 함수를 작성하려고 시도하고 사용자에게 배열의 값 (ints)을 입력하도록 요청합니다.사용자 정의 배열 길이의 배열 값 저장
샘플 출력은 원하는 :
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);
}
사용자가 정의한 길이의 배열에 대한 입력을 수집하는 방법을 이해하지 못합니다. 모든 설명을 부탁드립니다!
당신은 index-out-bound 문제가 있습니다. 'while (index
읽기 : [C에서 배열을 인쇄 할 때 이상한 동작?] (http://stackoverflow.com/questions/18009725/weird-behavior-when-printing-array-in-c/18009736#18009736) –