Im new in C and tryig to split user input string of integers and store them in dynamic table of int[]. If i print out the atoi(token) value - it shows required integer. But when trying to assign it to table - im getting values like below. Im doing something wrong with table, because when trying to pass tabSize to table cells im getting similar result. I will appreciate any insight, i've searched the web but didn't find similar issues.
What i ment to achive is:
//Input:
1,2,3,4
//Output:
tab[0] == 1
tab[1] == 2
tab[2] == 3
tab[3] == 4
What im getting is:
//Output:
tab[0] == 0
tab[1] == 0
tab[2] == 0
tab[3] == 4
Below code im trying:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *input;
int *tab;
int tabSize = 0;
scanf("%s", &input);
char *token = strtok(&input, ",");
while(token != NULL)
{
tab = (int*)calloc((tabSize + 1), sizeof(int));
tab[tabSize] = atoi(token);
token = strtok(NULL, ",");
tabSize++;
}
for (int i = 0; i < tabSize; i++)
{
printf("\n%d", tab[i]);
}
free(tab);
return 0;
}`
Thank you in advance