Is my struct statement wrong?

Asked

Viewed 276 times

1

#include <stdio.h>
#include <stdlib.h>
fun1(int ptr,int data)
{
    data.ano=1990;
    data.mes=5;
    data.dia=15;
    ptr->nome="Bigown";
    ptr->cpf="040502016";
}

int main ()
{
typedef struct {
    int ano;
    int mes;
    int dia;
} Data;
struct pessoa {
    char nome[20];
    char [12];
    Data data_nasc;
} x,*ptr;
    func1(*ptr,data);
    printf ("Data : %d/%d/%d \nPessoa:%s\nCPF:%s ",data.dia,data.mes,data.dia,ptr->nome,ptr->cpf);

    system("pause");
    return 0;
}
  • specify your question better

  • 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.

2 answers

4

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.

  • Yes I’m a little lost in this mare, and a lot in c that I need to learn , I will spend my weekend studying and reviewing what Oce teach me bigown, still missing a small part of the exercise. This printf should be invoked from a function and not in the main, I will try to solve this part of the exercise, since Oce gave me the path, I will strive to finish. Thank you

  • 1

    The ideal is to ask questions with more limited problems, normally your question would even be closed with so many problems that have. It is okay to ask several questions, problem is not to have a specific problem in each of them. You can ask about error, about concept you don’t understand, anything, as long as it’s programming, it’s specific, the question is asked clearly and doesn’t ask for opinions, it needs to be objective.

  • bigown I managed to accomplish the last step that and print using the function the values of the structure . I have understood everything that I have shown so far and I will continue programming to have fixation on the content taught, I thank too much friend.

3


It is good practice to define the separate types of structs. First we define the types and then we define the structs. This avoids circular dependencies and makes code more readable.

This technique is called Forward Delaration. To Wikipedia has an article talking more about such technique.

Here’s another solution to your problem:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* Forward Declarations */
typedef struct data_s data_t;
typedef struct pessoa_s pessoa_t;


struct data_s
{
    int ano;
    int mes;
    int dia;
};


struct pessoa_s
{
    char nome[20];
    char cpf[12];
    data_t data_nasc;
};


pessoa_t * obter_pessoa( void )
{
    pessoa_t * p = malloc(sizeof(pessoa_t));

    strcpy(p->nome, "Maniero");
    strcpy(p->cpf, "040502016");
    p->data_nasc.ano = 1990;
    p->data_nasc.mes = 5;
    p->data_nasc.dia = 15;

    return p;
}


void exibir_pessoa( pessoa_t * p )
{
    printf("Data: %d/%d/%d\n", p->data_nasc.dia, p->data_nasc.mes, p->data_nasc.ano );
    printf("Pessoa: %s\n", p->nome );
    printf("CPF: %s\n", p->cpf);
}


int main( int argc, char * argv[] )
{
    pessoa_t * p = obter_pessoa();

    exibir_pessoa(p);

    free(p);

    return 0;
}

/* fim-de-arquivo */

I hope I’ve helped!

  • help too much that beautiful code to read, thanks

Browser other questions tagged

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