how to pass two structs by function

0

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

  

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

The function stays like this (I do not know if it will work because I could not even access it by mistake)

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];
    }

}
    
asked by anonymous 16.05.2018 / 02:02

1 answer

0

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

Passing struct to function by value:

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

  return a;
}

Passing struct to function by reference:

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

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

Here is the complete 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;
}

Entry:

  

Type the number (By Value): 122
  Enter the registration number (By reference): 22

Output:

  

By value (Enrollment): 122

     

By reference (Enrollment): 22

See working at repl.it .

You can check out more here .

    
16.05.2018 / 04:37