May 15, 2014 at 5:00pm UTC
I need to read numbers from stdin into int array.
I am testing this with my program and it's not working. The array doesn't seem to have all the numbers in it. Can someone help me with this.
1 2 3 4 5 6 7 8
int input;
int count = 0;
int * tabela = NULL;
while (scanf ("%d" , &input) != EOF){
count++;
tabela = (int *) realloc (tabela, count * sizeof (int ));
tabela[count-1]=input;
}
P.S. The program works with this kind of array call
int tabela[5] = {1,2,3,4,5};
Last edited on May 15, 2014 at 5:02pm UTC
May 15, 2014 at 5:58pm UTC
I don't know why but count works. I used sizeof(tabela)/(sizeof(tabela[0])) do you maybe know why this didn't worked? And thank you.
May 15, 2014 at 6:28pm UTC
tabela is a pointer. Size of tabela will always be 4 (32 bit enviroment) or 8 (64 bit)
size of tabela[0] is a size of int and usually is 4. So you would realloc either 1 or 2 bytes.