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 .