How to declare in the function a component of a static struct? W

2

In the .h file:

  struct conectores {
    int entrada[n];
    int busca[n];
} conect;

struct connectors;

void cadastra_entrada(FILE *fp, int cont, struct conectores conect, int entrada[n]);

Function:

    void cadastra_entrada(FILE *fp, int cont, struct conectores conect, int entrada) {
int i;
// Preciso fazer com que não sobrescreva o arquivo também cada vez que entre na função
    for(i = 0; i < n; i++) {
        printf("Cadastrando:\n");
        fprintf(fp, "%d ", conect.entrada[i]);
    }        
    fprintf(fp," - padrão ");
    fprintf(fp, "%d", cont);
fprintf(fp, "\n");

}

Function call:

int main() {

int i, aux, op = 0;
int dig = 0;
int cont = 0;
int fim = 1;
FILE *fp;

struct conectores conect;

fp = fopen("conectores.txt", "rw");

// Leitura dos conectores
    aux = 0;
    for (i = 0; i < n; i++) {
        setbuf(stdin, NULL);
        scanf("%d", &conect.entrada[i]);
        printf("%d ", conect.entrada[i]);
    // Verifica se é 0 0 0 0 0 
        if (conect.entrada[i] == 0) {
            aux++;
            if (aux == n) {
                fim = 0;
                printf("FIM\n");
            } else {
                fim = 1;
            }   
        }
    }
    printf("Saí do for\n");
    while (fim != 0) {
    // Pega cada caracter e armazena em dig, busca se tem no arquivo 
    while ((dig = fgetc(fp)) != EOF) {
        printf("Entrei no while\n");
        i = 0;
        conect.busca[i] = dig;
        if (conect.busca[i] == conect.entrada[i]) {
            i++;
        } else {
            printf("Conector não encontrado. Gostaria de cadastrá-lo? s(1) ou n(2) \n");
            scanf("%d\n", &op);

            switch(op) {
                case(1):
                    cont = cont++;
                    cadastra_entrada(fp, cont, conect, entrada);
                    break;
                case(2):
                    printf("Novo padrão não cadastrado\n");
                    //imprime_inverso();
                    return 0;
                    break;
                default:
                    printf("Opção inválida\n");
                    break;
            }
        // Pula pra próxima linha para procurar na próxima linha ?????
            fscanf(fp, "\n");
        }



// inverte_valores(conect);
// imprime_inverso();
    }
    printf("t\n"); 
fclose(fp);
}

}

But it still gives the message that the entry is not declared. I tried with pointer, as vector, just as 'int input' and still did not give.

Thank you.

    
asked by anonymous 02.12.2015 / 13:36

2 answers

1

Deletes the definition of the variable conect of file h.

// ficheiro h
struct conectores {
    int entrada[n];
    int busca[n];
}; // sem definicao de variaveis deste tipo

Pass this setting to the c file.

// ficheiro c
struct conectores conect; // define a variavel conect,
                          // possivelmente dentro da função main()
    
02.12.2015 / 13:45
0

When you define a structure like this:

struct ponto {
    int x;
    int y;
}

You can declare a variable of this type like this:

struct ponto variavel;

Then you can access the numbers x and y like this:

variavel.x = 10;
variavel.y = 20;
printf("coordenada x do ponto: %d\n", variavel.x);
printf("coordenada y do ponto: %d", variavel.y);

The above code snippet will print the following:

  

x coordinate of the point: 10

     

y coordinate of the point: 20

But in your case the structure is defined like this:

struct conectores {
    int entrada[n];
    int busca[n];
} conect;

First of all you are defining a structure (called conectores ) and then declaring a variable (called conect ) with this type, so you do not need to type struct conectores conect; in main . Second, in your code you do not show something like #include "nome_do_arquivo_que_define_conectores.h" or anything like that. Also, where is the n that you use in the conectores structure?

Now let's see your registration function. It's prototyped like this:

void cadastra_entrada(FILE *fp, int cont, struct conectores conect, int entrada[n]);

But when you supply the function code you specify the parameter entrada like this: int entrada , where you place brackets in the function prototype. After all, what does the entrada parameter represent? What did you mean by that? The parameter conect already comes with a entrada . See the section below where you call the cadastra_entrada function.

switch(op) {
    case(1):
        cont = cont++;
        cadastra_entrada(fp, cont, conect, entrada);
        break;
...

Here you pass entrada to the function without first declaring entrada like this:

int entrada[n];
// faz algo com entrada
cadastra_entrada(fp, cont, conect, entrada);

or so (I do not know which one you meant):

int entrada;
// faz algo com entrada
cadastra_entrada(fp, cont, conect, entrada);

Remember that the conectores structure already defines a entrada but you say it has another entrada separated. Is that what you meant?

    
07.12.2015 / 15:09