-2
I’m doing an exercise to sequence numbers using a struct, but what’s happening is that in my for
the variable i
is being incremented in a very strange way, in the second passage of scanf
, its value becomes 104352
, instead of becoming 1
... I can’t understand why, but I imagine it’s the way I stated numeros[]
in struct
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int qtdeNumeros;
int qtdeRepetidos;
float numeros[];
}Sequencia;
int main()
{
Sequencia sequencia;
printf("Quantos numeros deseja colcoar na sequencia? ");
scanf("%d", &sequencia.qtdeNumeros);
for(int i=0; i < sequencia.qtdeNumeros; i++){
printf("Digite o numero [%d]: ", i+1);
scanf("%f", &sequencia.numeros[i]);
}
for(int i = 0; i<sequencia.qtdeNumeros; i++){
printf("Numeros [%d]: %f\n", i+1, sequencia.numeros[i]);
}
return 0;
}
You wouldn’t need to initialize
sequencia
?– rLinhares
Initialize in what sense?
– Ravel Sbrissa Okada
like this:
Sequencia *sequencia;
sequencia = malloc(sizeof(Sequencia));
– rLinhares
I even understood what you meant, but we can not use malloc since it was not a concept learned
– Ravel Sbrissa Okada
So there’s no way to fix it this way.
– Maniero