How to check if the vector of a record is empty in C

1

I have this record:

typedef struct{
    char nome[200], telefone[200], email[200];
} Agenda;

Agenda contato;

Agenda vetor[300];

And I want to print only those records that have data:

int lista_contatos(void){
    printf("========== Contatos ==========\n");

    for(i=0;i<300;i++){

        if(vetor[i].nome == NULL){
          printf("Contatos Listados.");
          main();  
        }else{
            printf(sizeof(vetor[i].nome));
            printf("Nome: %s \n",vetor[i].nome);
            printf("Telefone: %s \n",vetor[i].telefone);
            printf("Email: %s \n",vetor[i].email);
            printf("________________________\n");


        }       
    }   
}
    
asked by anonymous 29.10.2018 / 15:55

1 answer

2

The question does not give details and does not put important parts to help with more property, also because it may have errors in the part not shown (there are signs that have errors), but in essence in the presented form, which does not seem ideal, create a pattern, at the time you create the vector you must initialize all elements with an invalid value, for example you can put a null ( NULL ) in the three strings in all elements, this indicates that not valid data. You have to ensure that if a data is entered it can not be empty, it must have at least one space. At the time of access check the string size, if it is 0 it is because the element is empty.

You may be thinking that you do not need to initialize the 3 like this, and in fact for what asks in the question only need one of them, but to leave in order in all cases it is better to do in 3, do not use something without initializing

As you have opted for an array inside the structure it does not have a pointer and it does not make sense to use %code% , you can but to check if the first character of string is null, which is what I indicated to do above, there would do:

if (vetor[i].nome[0] == NULL) {
    
29.10.2018 / 16:05