Dynamic array in C

0

I have this little code of a dynamic array exercise, it introduces me up to 6 elements and it displays them correctly, but if I put it from 8, it jumps an error "the program stopped working"

Code:

int main()
{
    int cantidad;
    int *x;

    printf("Cantidad de valores del array: ");
    scanf("%d",&cantidad);

    x = (int*)malloc(sizeof(int));

    for(int i=0;i<cantidad;i++)
    {
        printf("Numero: ");
        scanf("%d",&x[i]);
    }

    //Visualizar Datos
    for(int i=0;i<cantidad;i++)
    {
        printf(" %d",x[i]);
    }

    return 0;
}
    
asked by Mario Guiber 03.09.2017 в 19:17
source

1 answer

1

Good morning,

I think the problem is that you are not reserving space for the storage of information. Try with:

x = (int *) malloc (sizeof (int) * amount);

Regards,

    
answered by 03.09.2017 / 19:24
source