Problems accessing struct members

Asked

Viewed 41 times

0

I have to do some functions with these structs, but since Agenda has a contact struct array and in each contact has another data struct, how do I access the fields? Do I have to work each struct separately? Thanks in advance, follow the code.

typedef struct Data{
    int dia,mes,ano;
}t_data;

typedef struct Contato{
    char nome[40];
    char fone[12];
    int idade;
    t_data dataNascimento;
}t_contato;

typedef struct Agenda{
    t_contato contatos[200];
}t_agenda;
  • I don’t know if I understand your doubt well. In the way you’re doing, I think it would be to have a variable like t_contato being by name Agenda. Something like: t_contato Agenda[200]. You would access: Agenda[indice].dataNascimento.Dia, for example.

  • Actually there were only two structures Contact and Agenda, but in the struct Contact there was this member that I understood as another struct: dataNascimento: Data(Day, month and year int). I was trying to answer as stated, but I think I’ll follow the way you suggested! Obg o/

  • Yes, it really is another structure. And it’s normal to have one structure inside another. But I see the way I said more "correct" because then, all your agendas would have a limit of 200 contacts, could not be more or less. In the way you said, you can have an agenda with N contacts. For example> t_contato AgendaDois[10]. Unless the contact structure within Agenda was dynamic. I believe that someone with more conceptual knowledge can respond better soon.

  • Dude you started doing your job ? to manipulate the STRUCT, if they are going to be passed by value or reference, if for value has a type of access the variable is by reference has another type, Note create function with VOID example: http://linguagemc.com.br/funcao-com-pass-referenced/

  • Yes! I saw some examples and I was aware of how to do it, but I was getting caught up in this part of accessing the structs the way they were presented.

1 answer

0


supposing you declared a t_agenda as written below, to access an element of the vector contatos, you must do as follows:

//declaração da variavel
t_agenda agenda;
//acessa o dia do t_data, onde `indice` deve ser substituido pelo valor do indice
agenda.contatos[indice].dataNascimento.dia;

Browser other questions tagged

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