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

Asked

Viewed 324 times

2

In the file . h:

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

struct connectors conect;

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, only as 'int input' and not yet.

Thank you.

  • But you did include do h in the header of your file . c? Example: #include "Lib.h"

  • Uhum :/ If I wasn’t going to be giving much more errors, struct etc. But it’s only this variable that I’m not able to pass by reference.

  • Shows the function that makes the call from cadastra_entrada(); the variable entrada must be defined within that function (or be global, which is a bad idea)

  • It is in the main same. I edited the description of the question with the program.

  • In function main() there is no variable entrada. What exists is conect.entrada[X].

2 answers

1

Removes the variable definition conect from file h.

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

Pass this setting to file c.

// ficheiro c
struct conectores conect; // define a variavel conect,
                          // possivelmente dentro da função main()
  • But when I declare in the function, I declare as: void cadastra_input(FILE *Fp, int cont, struct connectors conect, int input) same? Pq it is still saying that in function call the variable 'input' is not declared

  • connectors. c:82:42: error: voltar input' undeclared (first use in this Function) cadastra_input(Fp, cont, conect, input);

  • In the function you have to declare a variable with the type of your struct like this: void cadastra_entrada(FILE *fp, int cont, conect NomeVar, int entrada)

0

When you define a structure like this:

struct ponto {
    int x;
    int y;
}

You can declare a variable of this type:

struct ponto variavel;

Then you can access the numbers x and y thus:

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

The code snippet above will print the following:

point x coordinate: 10

point coordinate y: 20

But in your case the structure is defined like this:

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

First that this way you are defining a structure (called conectores) and then declaring a variable (called conect) with this guy, so you don’t have to write struct conectores conect; in the main. Second in your code you don’t show something like #include "nome_do_arquivo_que_define_conectores.h" or anything like that. Also, where the n that you use in the structure conectores?

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 provide the code of the function you specify the parameter entrada thus: int entrada, and in the prototype of the function you place brackets. After all, what represents the parameter entrada? What did you mean by that? The parameter conect already comes with a entrada. See the section below where you call the function cadastra_entrada.

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

Here you pass entrada to the function without first declaring entrada thus:

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

or so (I don’t know which one you meant):

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

Remember that the structure conectores already defines a entrada but you say you have another entrada separate. That’s what you meant?

Browser other questions tagged

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