Width Search - error: 'str_no' undeclared (first use in this Function)

Asked

Viewed 336 times

4

I have the following code on C, for a wide search:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

//Variáveis globais
int id = 0;
int proximo = 0;

//Função de Busca em Largura
int buscaLargura(int *grafo, int alvo, int inicio, int tamanho)
{
    struct str_no fila[tamanho];
    int indice = 0;
    int achou = 0;

    //Procura nó inicial
    while(achou == 0)
    {
        if(grafo->id == inicio)
        {
            achou = 1;
        }
        else
        {
            grafo = grafo->proximo;
        }
    }
    achou = 0;
}

//Procura o nó alvo
fila[indice] = grafo;
indice++;
while(indice > 0 && achou == 0)
{
    if(grafo->id == alvo)
    {
        achou = 1;
    }
    else
    {
        while(grafo->proximo != NULL)
        {
            grafo = grafo->proximo;
            fila[indice] = grafo;
            indice++;
        }
        //Desenfileira
        grafo = pilha[0];
        for(int i = 1; i < indice; i++)
        {
            pilha[i-1] = pilha[i];
        }
        indice--;
    }
    return(grafo);
}

Whenever I try to run, the compiler "Codeblocks" returns the following ERROR:

||=== Build file: "no target" in "no project" (Compiler: Unknown) ===| In Function 'buscaLargura':| |17|error: array type has incomplete element type| |24|error: request for Member 'id' in Something not a Structure or Union| |30|error: request for Member 'next' in Something not a Structure or Union| |error: 'Indice' undeclared here (not in a Function)| | 37|Warning: data Definition has no type or Storage class| |37|error: 'graph' undeclared here (not in a Function)| |38|error: expected '=', ',', ';', 'asm' or 'attribute' before '++' token| |39|error: expected Identifier or '(' before 'while'| ||=== Build failed: 7 error(s), 1 Warning(s) (0 minute(s), 0 Second(s)) ===|

2 answers

0

There are several errors in your program:

|17|error: array type has incomplete element type|

The error is on the line:

struct str_no fila[tamanho];

The first error is that you have not declared the type "struct str_no" and are already trying to create a variable of this type. Another error is that you cannot declare an array static using a size dynamic. Or you set a fixed size:

struct str_no fila[10];

Or you should dynamically allocate the array:

struct str_no *fila = malloc(sizeof(*fila) * tamanho);

|24|error: request for Member 'id' in Something not a Structure or Union|: |30|error: request for Member 'proximo' in Something not a Structure or Union|

The variable graph is a pointer to int, and not a structure. Therefore, it makes no sense to access members called id and next, that do not exist: graph->id.

I won’t even mention the other errors. Clearly you need to study the syntax of the C language.

  • Thank you very much, I am doing this, tirelessly studying this programming language, your tips and explanations have served me very

  • On static array with dynamic value, actually you can. Maybe it’s not C89, but C99 already is. And it’s considered static allocation

0

The first point here is: what is a struct str_no? At the moment, the compiler has no idea what it is. How are you trying to create an array with tamanho elements, the compiler will take an undetermined value and multiply by the parameter tamanho to allocate the memory needed for the vector to exist... and, well, it gives error...

Another point is that grafo is a pointer to integer, not to struct str_no, then it has no attributes for you to refer to with the arrow operator ->. Therefore, grafo->id is not feasible.

Finally, there is the problem of the premature end of the function. I believe that all this part since the fila[indice] = grafo; should be the continuation of the function. But, it has a key lock indicating that it finished a block just before. As the only open block is the one of the function buscaLargura, lock keys terminates this function. Everything found after that is in the global context. And as I recall in the global context, you can only have statements, whether variable, type or function, no commands. Errors from row 37 can be interpreted as collateral errors to premature function closure.

Even by placing the remaining block of loose code within the function, there is still the case that within the for you use an undeclared variable called pilha. I believe you wanted to use the fila here, correct?


Okay, we’ve talked a lot about mistakes, and how about talking about fixing?

The first step is to declare the structure before of the creation of the function buscaLargura. This can be done by copying and pasting the struct str_no { ... } for the beginning of the file or, if it is separating into multiple source files, importing the .h necessary.

The second correction is to correct the type of int *grafo for struct str_no *grafo in the function parameters buscaLargura.

The third point is to place the loose code snippet inside the function. Also taking advantage of the third point, remove mention of the variable pilha; should be fila in place.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.