I started working on a program and I need to create function, that reads input, in format {number,number, ...} and puts numbers into dynamically allocated array. I am currently having trouble reallocating the array. Here is the code. The program compiles with no errors or warnings
int readInput(int ** znamky, int *count )
{
size_t n = 1;
char overeni[2];
char znaminko[2];
char carka [2] = ",";
char zavorka[2] ="{";
char zavorka2[2] ="}";
printf("Pocty bodu:\n");
scanf(" %c",&overeni[0]); // I check if first character is {
if (overeni[0]!=zavorka[0])
{
printf("Nespravny vstup.\n");
return EXIT_FAILURE;
}
while (scanf("%d",znamky[*count])==1) // here I enter the cycle and load first number
{
if (scanf(" %c",&znaminko[0])==1) // if char after number is ","
{
if (znaminko[0]==carka[0])
{
printf("sdf\n");
*count = *count + 1; // I append lenght of array
if (*count==n) // if the length is same as currently allocated memory
{
n = n*2;
*znamky = (int*) realloc (*znamky, n * sizeof(int) + 4);
// Here I am trying to reallocate the memory and when using debugger it probably goes ok, but //the problem is when I repeat the cycle for the second time because I get seg fault
}
continue;
}
if (znaminko[0]==zavorka2[0])
{
*count = *count + 1;
printf("utikam\n");
return 1;
}
}
else
{
printf("Nespravny vstup\n");
return EXIT_FAILURE;
}
}
return 1;
}
int main (void)
{
int *znamky = (int*) malloc (sizeof(int));
int count = 0;
if (znamky == NULL)
{
free (znamky);
return EXIT_FAILURE;
}
readInput(&znamky, &count);
return 0;
}
So I was wondering if someone could explain to me what is happening inside the program, I still feel a little bit lost with pointers, and passing arrays to functions. Thank you very much...