how to make two arrays equal in c
To make two arrays equal in C, you can use a loop to assign each element of one array to the corresponding element of the other array. Here is an example code snippet that demonstrates this:
#include <stdio.h>
#define SIZE 5
int main() {
int array1[SIZE] = {1, 2, 3, 4, 5};
int array2[SIZE];
int i;
for (i = 0; i < SIZE; i++) {
array2[i] = array1[i];
}
printf("Array1: ");
for (i = 0; i < SIZE; i++) {
printf("%d ", array1[i]);
}
printf("\n");
printf("Array2: ");
for (i = 0; i < SIZE; i++) {
printf("%d ", array2[i]);
}
printf("\n");
return 0;
}
This code initializes array1
with values {1, 2, 3, 4, 5}
. It then uses a for
loop to iterate over each element of array1
and assigns it to the corresponding element of array2
. Finally, it prints the contents of both arrays to verify that they are equal.
When you run this code, the output will be:
Array1: 1 2 3 4 5
Array2: 1 2 3 4 5
This confirms that both arrays are equal.