The code has several errors that I will list. To learn you need to understand what you are doing. You have to read the compiler errors, interpret them, look for them and find a solution. That's what all programmers do. Of course the ideal is to study the language deeply, make the code carefully and avoid mistakes. It is not always possible, of course, but if the code is done knowing what it is, carefully, in an organized way, the compiler will give the errors and you can fix at least the basics.
You need to learn from the mistakes and do not re-commit them in subsequent codes, as it happened here.
There were several basic syntax errors, errors like name in the function declaration or structure member, and then use of another name in the call and assignment. There was even an unnamed member error ( cpf
).
The code follows little or no coherent and plausible structure. You even have unused variables.
The first change I made was to separate the data structure from the algorithm. I left the structures out of the function to be used throughout the code. I also created types with typedef
to make the job easier later. This is how you usually do it in real code. Note that I preferred to use capitalized names for the types. Pure convention for easy reading later.
I've created a variable to store a person's data and I've allocated the memory as it appears to be (you would not need to use pointer and allocate memory in heap , this is just one of the options, then maybe it is an exercise requirement).
I passed this variable to the function in a simple way. She's already a pointer. You do not need to create and pass a variable data
because the use of the date is inside the person, it is not something independent. I might as well, but unnecessary. But if it is going to happen, then the variable would have to be created before, it can not pass what does not exist.
In the function that will assign the data has an error because the return type is missing (in case it does not have a return it would be void
). In addition to needing only one parameter, it should be of type Pessoa
and not int
. You can not kick things. Note that it was not necessary to use struct
before Pessoa
because it has now been declared as a data type.
Within the function, the data is only assigned to the structure. The first error is that the date is being handled independently. Since the date is part of the structure of the person, it has to access each of its members through the variable pessoa
(note that I preferred to use a more meaningful name than the fact variable). Then I access the variable pessoa
, there the member data_nasc
(I got the name) and inside it I accessed its members dia
, mes
and ano
. I just needed to use the .
operator instead of ->
because in this case I'm accessing the data directly and not through a pointer.
The other two data are wrong because in C a string can not be assigned directly to a variable, you must copy its characters with the strcpy()
.
Finally, I set up printf()
which had minor syntax problems in the arguments and formatting.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int ano;
int mes;
int dia;
} Data;
typedef struct {
char nome[20];
char cpf[12];
Data data_nasc;
} Pessoa;
void func1(Pessoa *pessoa) {
strcpy(pessoa->nome, "Bigown");
strcpy(pessoa->cpf, "040502016");
pessoa->data_nasc.ano = 1990;
pessoa->data_nasc.mes = 5;
pessoa->data_nasc.dia = 15;
}
int main() {
Pessoa *pessoa = malloc(sizeof(Pessoa));
func1(pessoa);
printf("Data: %d/%d/%d\nPessoa: %s\nCPF: %s ", pessoa->data_nasc.dia, pessoa->data_nasc.mes, pessoa->data_nasc.ano, pessoa->nome, pessoa->cpf);
return 0;
}
See running on ideone .
Of course, if the date is something merely descriptive and you do not need to use it for anything else, you can use it just as if it were a string , just like you did with CPF.