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, search for them and find a solution. That’s how all programmers do it. Of course the ideal is to study the language deeply, do the code carefully and avoid errors. It is not always possible, of course, but if the code is made knowing what it is about, carefully, in an organized way, the compiler will give the errors and can be fixed at least the basic.
You need to learn from errors and not commit them in subsequent codes, as occurred here.
There were several very basic syntax errors, errors like name in function statement or structure member, and then use another name in call and assignment. There was even an unnamed member error (cpf
).
The code follows little or no coherent and plausible structure. It even has unused variables.
The first change I made was to separate the data structure from the algorithm. I left the structures outside the function to be used throughout the code. I also created guys with typedef
to facilitate the work later. This is how it is usually done in real code. Note that I preferred to use names in upper case for the types. Pure convention to facilitate reading afterwards.
I created a variable to store a person’s data and stored the memory, as the intention seems to be (I would not need to use pointer and allocate memory in heap, this is just one of the options, following perhaps is a requirement of the exercise).
I passed this variable to the function simply. It is already a pointer. No need to create and pass a variable data
because the use of the date is within the person, it is not something independent. It could, but unnecessary. But if it will pass, then the variable would have to be created before, it cannot pass what does not exist.
In the function that will assign the data has error because it lacks the type of return (in case for not having a return would void
). Besides needing only one parameter, it should be the type Pessoa
and not int
. You can’t kick things. Note that you didn’t have to use struct
before Pessoa
, because now it has been declared as a kind of data.
Within the function only the data is assigned to the structure. The first error is that the date is being treated independently. As the date is part of the person’s structure, it has to access each of its members through the variable pessoa
(note that I preferred to use a more meaningful name than the actual variable). So I access the variable pessoa
, there the member data_nasc
(I got the name right) and within it I gave its members dia
, mes
and ano
. All I had to do was use the operator .
instead of ->
because in this case I am accessing the data directly and not through a pointer.
The other two dice are wrong because in C one string cannot be assigned directly to a variable, you need to copy its characters with the function strcpy()
.
Finally, I put the printf()
that 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, "Maniero");
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);
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Obviously, if the date is something merely descriptive and you don’t need to use it for anything else, you can just use it as a string, just like you did with CPF.
specify your question better
– Marcos Henrique
I put the tag C++ because in a previous question it showed that it is developing in a C++ compiler, and as the answer also serves those who are looking for C++, I think it helps the question to be found by other people.
– Maniero