4. Pointers and arrays
•Also, remember that array variables cannot be assigned of another
variable (we use the memcpy function instead):
Systems Architecture - 5. Pointers in C 24
#include <stdio.h>
#include <string.h>
#define SIZE 4
void display_array(int array[], int size) {
for (int i = 0; i < size; i++) {
printf("array[%d]=%d\n", i, array[i]);
}
printf("\n");
}
int main() {
int array_1[SIZE] = { 25, 50, 75, 100 };
int array_2[SIZE];
memcpy(array_2, array_1, sizeof(array_1));
display_array(array_1, SIZE);
display_array(array_2, SIZE);
return 0;
}
array[0]=25
array[1]=50
array[2]=75
array[3]=100
array[0]=25
array[1]=50
array[2]=75
array[3]=100
#include <stdio.h>
#define SIZE 4
int main() {
int array_1[SIZE] = { 25, 50, 75, 100 };
int array_2[SIZE];
array_2 = array_1; // forbidden
return 0;
}
arrays_5_error.c: In function ‘main’:
arrays_5_error.c:8:13: error: assignment to expression
with array type
8 | array_2 =array_1; // forbidden
| ^