how to pass two structs per function

Asked

Viewed 63 times

0

I have two structs, one for the student data and one for a hash table, where the collisions of the matricules go. To know if there was a precise collision of these two structs ,but dev says that there is some error when I try to pass the two to a function. this function below, this inside the insert,the error says

"expected Expression before students" tabela(alunos *al,thash *t);

The function looks like this(I don’t know if it will work because I couldn’t access it due to the error)

tabela(alunos *al,thash *t){
    alunos *p;
    thash *pp;
    int resto;

    for(p=al;p!=NULL;p=p->prox){

        resto=p->matri%4;
        pp->colisao[resto];
    }

}
  • What type of function tabela ? That part was missing. By the look of the code it would be type void

  • How are you calling the function tabela()?

1 answer

0

If there is no type defined for the struct it is necessary to inform explicitly the struct, basically like this struct aluno a. In addition, the passage of struct can be done by reference or by value. See examples below.

Passing a struct for function by value:

struct aluno lerAlunoValor(struct aluno a)
{
  printf("Digite o numero matricula (Por Valor): ");
  scanf("%i", &a.matricula);

  return a;
}

Passing a struct for function by reference:

void lerAlunoRef(struct aluno *a)
{
  printf("Digite o numero matricula (Por referencia): ");
  scanf("%i", &a->matricula);
}

Note that a new value is returned in the passage struct, already in the passage by reference to struct is modified within the function scope lerAlunoRef().

Follow the full example:

#include <stdio.h>

struct aluno 
{
  int matricula;
};

struct aluno lerAlunoValor(struct aluno a)
{
  printf("Digite o numero matricula (Por Valor): ");
  scanf("%i", &a.matricula);

  return a;
}

void lerAlunoRef(struct aluno *a)
{
  printf("Digite o numero matricula (Por referencia): ");
  scanf("%i", &a->matricula);
}

int main(void) 
{
  struct aluno a, b;

  a = lerAlunoValor(a);

  lerAlunoRef(&b);

  printf("\n\nPor valor (Matricula): %i\n", a.matricula);
  printf("\n\nPor referencia (Matricula): %i\n", b.matricula);

  return 0;
}

Entree:

Enter the number plate (Per Value): 122
Enter the number plate (By reference): 22

Exit:

By value (Registration): 122

By reference (Matricula): 22

See working on repl it..

You can consult more here.

Browser other questions tagged

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