I'm quite new (and quite awful) at c. Im trying to write a recursion that puts binary tree data into a dynamic table. In the given data, natural numbers > 0 represent nodes, zeroes represent NULL nodes and each node is followed by \n; (prefix left to right notation). So a guy without children is given like: 1\n0\n0
Some fragments of programm might seem unnecessarily (like oindeks) - they will be used in the future.
when i input data im given free(): double free detected in tcache 2 and process is terminated. I suppose its rather a problem with realloc, cause free() is present only in main, but I truly dont know. Thank you in advance for any kind of help!
void Sczytaj(int Drzewiec[], int* a, int* b, int* c) {
int Obecny, oindeks;
scanf("%d", &Obecny);
oindeks = *a;
Drzewiec[oindeks] = Obecny;
if (Obecny == 0) {
*a += 1;
}
if (Obecny > 0) {
*a += 1;
*b += 1;
*c += 2;
Drzewiec = realloc(Drzewiec, sizeof(int) * (size_t) *c); //should it be sizeof(int*)?
Sczytaj(Drzewiec, &*a, &*b, &*c);
Sczytaj(Drzewiec, &*a, &*b, &*c);
}
}
int main(void) {
printf("Wpisz dane:");
int wielkość = 1, indeks = 0, indeks_bez_zera = 0;
int* Drzewiec;
Drzewiec = (int*)malloc(sizeof(int) * (size_t) wielkość);
Sczytaj(Drzewiec, &indeks, &indeks_bez_zera, &wielkość);
for (int i = 0; i < wielkość; i++) {
printf("%d hej\n", Drzewiec[i]);
}
printf("\nOstatni indeks to: %d\nOstatni indeks_bez_zera to: %d\nWielkość to: %d", indeks, indeks_bez_zera, wielkość);
free(Drzewiec);
}
I tried changing the function type to int, but i sorta need to call it twice for every non-empty node (2 possible children), and Im not sure whether i can do it with an inclusion of return.