stdin to array

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
I see nothing wrong. If you print the array elements after the input loop does it not output correctly?
1
2
3
4
for (int i = 0; i < count; ++i)
{
	printf("%d\n", tabela[i]);
}
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.
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.
Topic archived. No new replies allowed.