Display the value of the vectors in C

0

Good afternoon,

I am a beginner and I am creating a program with 3 vectors and I would like the value typed to show me on screen, but when I run it, it shows some values that I did not type.

Could you help me:

#include<stdio.h>
#include<stdlib.h>

main()

{

  int i, A[i], c[i], mult=0, ctrl;
  char b[i];
  char comb;


i=0;
A[i]=0;
c[i]=0;
ctrl=0;

printf("Quantos vendas foram realizadas? : ");
scanf("\n%d", &ctrl);

//Entrada de dados



  for (i=0; i < ctrl; i++)
{

    printf("Digite a quantidade em Litros: ");
    scanf("\n%d", &A[i]);
    printf("A[%d] = %d\n", i, A[i]);

    printf("Digite o tipo do combustível: ");
    scanf("\n%c", &b[i]);
    printf("b[%d] = %c\n", i, b[i]);    

    printf("Digite o valor: ");
    scanf("\n%d", &c[i]);
    printf("c[%d] = %d\n", i, c[i]);  

    }
    for (i=0; i < ctrl; i++)
    {
    mult = A[i] * c [i];
    printf("i = %d valor multi: %d\n", i, mult);
    }    

    //Imprimindo os valores do vetor
    for (i=0; i < ctrl; i++)
    {
    printf("A[%d] = %d\n", i, A[i]);
    printf("b[%d] = %c\n", i, b[i]);
    printf("c[%d] = %d\n", i, c[i]);

    }



}
    
asked by anonymous 21.08.2018 / 18:39

1 answer

0

This should return segmentation fault (if i is 0)

i has no initial value (i is any value) if it is 0 then it is working with a null vector, if it is 1 then it is working with a 1-position vector.

If you do:

int i, A[i];

i has an alias value because it is allocated in an aladinary location in memory, ie, an earlier program that used that position may have freed up the space (without cleaning it) and left the value 5000, causing it to have a vector of 5000 positions.

So the correct one would be:

int i, A[100], c[200];...
    
21.08.2018 / 18:53