I have a pointer to a pointer ("paths") and I want to reallocate each pointer (each "path"). But I get a crash. Generally I am trying to find all possible powers of a number, which one can compute for some amount of operations (e.g for two operations we can get power of three and four (one operation for square of a number, then another one either for power of three or four)). I figured out how to do it on paper, now I am trying to implement it in code. Here is my try:
#include <stdio.h>
#include <stdlib.h>
void print_path(const int *path, int path_length);
int main(void)
{
    fputs("Enter number of operations? ", stdout);
    int operations;
    scanf("%i", &operations);
    int **paths, *path, npaths, npath;
    npaths = npath = 2;
    path = (int*)malloc(npath * sizeof(int));
    paths = (int**)malloc(npaths * sizeof(path));
    int i;
    for (i = 0; i < npaths; ++i)    // paths initialization
    {
        int j;
        for (j = 0; j < npath; ++j)
            paths[i][j] = j+1;
    }
    for (i = 0; i < npaths; ++i)    // prints the paths, all of them are displayed correctly
        print_path(paths[i], npath);
    for (i = 1; i < operations; ++i)
    {
        int j;
        for (j = 0; j < npaths; ++j) // here I am trying to do it
        {
            puts("trying to reallocate");
            int *ptemp = (int*)realloc(paths[j], (npath + 1) * sizeof(int));
            puts("reallocated");    // tried to write paths[j] = (int*)realloc...
            paths[j] = ptemp;   // then tried to make it with temp pointer
        }
        puts("memory reallocated");
        ++npath;
        npaths *= npath;    // not sure about the end of the loop
        paths = (int**)realloc(paths, npaths * sizeof(path));
        for (j = 0; j < npaths; ++j)
            paths[j][npath-1] = paths[j][npath-2] + paths[j][j];
        for (j = 0; j < npaths; ++j)
            print_path(paths[j], npath);
        puts("\n");
    }
    int c;
    puts("Enter e to continue");
    while ((c = getchar()) != 'e');
    return 0;
}
void print_path(const int *p, int pl)
{
    int i;
    for (i = 0; i < pl; ++i)
        printf(" A^%i -> ", p[i]);
    puts(" over");
}
 
     
  
     
    