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

3

I have the following code in C , for a search in width:

#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 "CodeBlocks" compiler returns the following ERROR :

  

|| === Build file: "no target" in "no project" (compiler: unknown) === |   In function 'searchlight': | | 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 'proximo' in   something not a structure or union | error: 'index' 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)) === |

    
asked by anonymous 21.06.2016 / 01:08

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 you are already trying to create a variable of this type. Another mistake is that you can not declare a static array using a dynamic size. 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 'next' in something not a structure or union |

The graph variable is a pointer to int , not a structure. So it does not make sense to access members called id and next , which do not exist: graph-> id .

I will not even mention the other mistakes. Clearly you need to study the C language syntax.

    
31.10.2016 / 20:19
0

The first point here is: what is a struct str_no ? At the present time, the compiler has no idea whatsoever. Since you are trying to create a vector with tamanho elements, the compiler will get an undetermined value and multiply by the parameter tamanho to allocate the memory required 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 , so it does not have attributes for you to refer with the arrow -> operator. Therefore, grafo->id is not feasible.

Finally, there is the problem of premature termination of the function. I believe that all this part from fila[indice] = grafo; should be the continuation of the function. But, it has a dated keys indicating that it finished a block sooner. Because the only open block is the buscaLargura function, the closing keys ends this function. Everything that lies after that is in the global context. And from what I remember in the global context you can only have declarations, whether of variables, types or functions, no commands. Errors from line 37 can be interpreted as side-effects to premature closure of the function.

Even putting the remaining block of loose code inside the function, there is still the case that within for you use an undeclared variable called pilha . I think you wanted to use fila here, right?

Okay, we talked a lot about mistakes, and how about talking about how to fix it?

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

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

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.

    
22.09.2017 / 07:15