Basically, I am attempting to write a simple C function that prompts the user for an array length, and then asks the user to input values (ints) for the array.
Sample Output Desired:
Enter Array Length: 5
Enter values for the array:
1 2 3 6 7
The current array is:
1 2 3 6 7
Here is my code at the moment. I feel as if this is should work, but with such a basic knowledge of C, it's causing a segmentation fault.
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);
}
I don't understand how to collect input for an array that is of length that a user defines. Any explanation is appreciated!