My question is simple if I have to input a data in an array with size user defined in C should I use pointers to store them or arrays
#include <stdio.h>
#include <stdlib.h>
int main(){
int *arr, n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
arr=(int *)malloc(n*sizeof(int));
printf("Enter number of elements:\n");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
}
Or
#include <stdio.h>
#include <stdlib.h>
int main(){
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter number of elements:\n");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
}
Personally I find it more comfortable to use pointers, but as C treats array and pointers differently I may face problems in the future.
nthe second one could end up overflowing the stack, breaking your code