Are there any limitations when implementing scanf
int data type to an array in C? The code below gives me a "segmentation fault(core dumped) when array has more than 6 indices.
====example code=========
#include <stdio.h>
int main(void)
{
int size;
int array[size];
int x;
scanf("%d", &size);
for(x = 0; x < size; x++){
scanf("%d", &array[x]);
}
for(x = 0; x < size; x++){
printf("[%d] ", array[x]);
}
}
input:
4
3 45 5 76
output:
[3] [45] [5] [76]
input:
7
34 2 5 6 9 3 8
output:
Segmentation fault(core dumped)
My goal is to have a dynamic array. The user will decide its size. So far this proves impossible. I understand the dynamic array approach can be implemented with struct
or c++ vector
. However, I would like to understand whether the error is due to the code structure or am I ignorant of certain limitation in the C array.
I look forward to your knowledgeable input.