My struct declaration is wrong?

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;
}
    
asked by anonymous 04.05.2016 / 20:15

2 answers

1

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

This technique is called Forward Delaration . The Wikipedia has an article talking more about this technique.

Here's one more 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, "Bigown");
    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 have helped!

    
07.05.2016 / 00:32
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, 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.

    
04.05.2016 / 20:53