Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
Defining a recursive function inside another function in C

I'm trying to write a recursive function (printPath) inside another function (dijkstraSSSP), but it gives me a compiler error.

When I run gcc dijkstra.c WGraph.c PQueue.c, it compiles just fine. However, when I run dcc -Wall -Werror -std=c11 -o dijkstra dijkstra.c WGraph.c PQueue.c. I got this error:

dijkstra.c:48:38: error: function definition is not allowed here
void printPath (int currentNode) {

Which I think it is because I define a function inside another function. If this is the case, how should I modify the code? Thanks a lot!

void dijkstraSSSP(Graph g, Vertex source) {
    int  dist[MAX_NODES];
    int  pred[MAX_NODES];
    bool vSet[MAX_NODES];  // vSet[v] = true <=> v has not been processed
    int s, t;

    PQueueInit();
    int nV = numOfVertices(g);
    for (s = 0; s < nV; s++) {
        joinPQueue(s);
        dist[s] = VERY_HIGH_VALUE;
        pred[s] = -1;
        vSet[s] = true;
    }

    dist[source] = 0;  

    int relaxWeight;
    for (s = 0; s < numOfVertices(g); s++) {
        s = leavePQueue(dist);
        //printf("iterating with s = %d\n", s);
        //printf("updating vSet[%d] = false\n", s);
        vSet[s] = false;
        for (t = 0; t < numOfVertices(g); t++) {
            //printf("iterating with t = %d\n", t);
            //printf("checking if s = %d is adj to t = %d and vSet[%d] = true\n", s, t, t);
            if (adjacent(g, s, t) != 0 && vSet[t] == true) {
                //printf("YES\n");
                relaxWeight = adjacent(g, s, t);
                if (dist[t] > dist[s] + relaxWeight) {
                    //printf("updating dist[%d] = dist[%d] + adjWeight[%d]\n", t, s, relaxWeight);
                    dist[t] = dist[s] + adjacent(g, s, t);
                    //printf("updating pred[%d] = %d\n", t, s);
                    pred[t] = s;
                }
            }
        }
    }
 
    void printPath (int currentNode) {
        if (pred[currentNode] == -1) {
            printf("%d", currentNode);
            return;
        } else {
            printPath (pred[currentNode]);
            printf("-%d", currentNode);
        }
    }

    for (int i = 0; i < numOfVertices(g); i++) {
        if (dist[i] == VERY_HIGH_VALUE) {
            printf("%d: no path\n", i);
        } else{
            printf("%d: distance = %d, shortest path: ", i, dist[i]);
            printPath(i);
            printf("\n");
        }
    }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Defining a function inside another function is not defined in the C Standard. gcc allows it as an extension, but most compilers don't.

You should move the definition of printPath outside the body of the dijkstraSSSP function, before it in the source code, and pass pred as an extra argument:

void printPath(const int *pred, int currentNode) {
    if (pred[currentNode] == -1) {
        printf("%d", currentNode);
    } else {
        printPath(pred, pred[currentNode]);
        printf("-%d", currentNode);
    }
}

void dijkstraSSSP(Graph g, Vertex source) {
    int  dist[MAX_NODES];
    int  pred[MAX_NODES];
    bool vSet[MAX_NODES];  // vSet[v] = true <=> v has not been processed
    int s, t;

    PQueueInit();
    int nV = numOfVertices(g);
    for (s = 0; s < nV; s++) {
        joinPQueue(s);
        dist[s] = VERY_HIGH_VALUE;
        pred[s] = -1;
        vSet[s] = true;
    }

    dist[source] = 0;  

    int relaxWeight;
    for (s = 0; s < numOfVertices(g); s++) {
        s = leavePQueue(dist);
        //printf("iterating with s = %d\n", s);
        //printf("updating vSet[%d] = false\n", s);
        vSet[s] = false;
        for (t = 0; t < numOfVertices(g); t++) {
            //printf("iterating with t = %d\n", t);
            //printf("checking if s = %d is adj to t = %d and vSet[%d] = true\n", s, t, t);
            if (adjacent(g, s, t) != 0 && vSet[t] == true) {
                //printf("YES\n");
                relaxWeight = adjacent(g, s, t);
                if (dist[t] > dist[s] + relaxWeight) {
                    //printf("updating dist[%d] = dist[%d] + adjWeight[%d]\n", t, s, relaxWeight);
                    dist[t] = dist[s] + adjacent(g, s, t);
                    //printf("updating pred[%d] = %d\n", t, s);
                    pred[t] = s;
                }
            }
        }
    }
 
    for (int i = 0; i < numOfVertices(g); i++) {
        if (dist[i] == VERY_HIGH_VALUE) {
            printf("%d: no path\n", i);
        } else{
            printf("%d: distance = %d, shortest path: ", i, dist[i]);
            printPath(pred, i);
            printf("\n");
        }
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

Move the definition of printPath outside of the body of dijkstraSSSP and, to give it access to pred, change it to accept two parameters instead of one. The additional parameter should have type int * and should point to the first element of pred.

In the call, pass pred for that new parameter. The array pred will be automatically converted to a pointer to its first element.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda