I'd like to count how much my recursion is nested and return every level number in an array created inside function, but that code doesn't work properly (it displays "Segmentation fault" when ran):
void test (int* result[], int* counter) {
if (*counter == 0)
*result = (int**) malloc(0);
if (*counter == 5)
return;
else {
*counter = *counter + 1;
*result = (int**) realloc(*result, sizeof(int) * (*counter));
*result[*counter - 1] = *counter; // This line is wrong, and I don't know why.
test(result, counter);
}
}
void main () {
int** result;
int counter = 0;
test(result, &counter);
int i;
for (i = 0; i < 5; i++)
printf("%d\n", *result[i]);
}
I know I could do it using single pointer as parameter and as return value. The point is, I'm just curious.