For some reason, reading input using scanf() won't work this way:
int main() {
int arr1_size = 0;
// Reading array size
scanf("%d", &arr1_size);
int* arr1 = (int*) malloc(sizeof(int) * arr1_size);
// Reading and printing array values
for(int i = 0; i < arr1_size; i++) {
scanf("%d", arr1);
arr1++;
}
for(int i = 0; i < arr1_size; i++) {
printf("%d", arr1[i]);
}
return 0;
}
What could be the issue?
arr1here:scanf("%d", arr1); arr1++;. I think more correctly,scanfis working as expected,printfis not.scanf("%d", arr1 + i);to preserve the pointer frommalloc. Thenprintfwill work correctly.mallocit would be better to go on onlyif(arr1_size > 0)