简体   繁体   中英

Learning how to properly reference to a struct field when using scanf (C)

I've made this struct

struct dadosPessoais
{
    char nome[60];
    char end[60];
    char cidade[60];
    char estado[3];
    int cep;
};
typedef struct dadosPessoais dadosPessoais

Also, I've declared these variable and pointer

dadosPessoais dp[4], *dpp;

What I'm currently trying to do is receive some data from user and using *dpp pointer in order to store this data within dp[] variable. To do it, I've made this loop structure

for (i = 0; i < 4; i++)
{
    dpp = &dp[i];
    printf("-- Dados da %da pessoa --\n", i + 1);
    printf("Nome: ");
    scanf("%s", dpp->nome);
    getchar();
    printf("Endereço: ");
    scanf("%s", dpp->end);
    getchar();
    printf("Cidade: ");
    scanf("%s", dpp->cidade);
    getchar();
    printf("Estado: ");
    scanf("%s", dpp->estado);
    getchar();
    printf("Cep: ");
    scanf("%d", dpp->cep);
    getchar();
    system("clear");
}

I know that using something like dpp->field is not correct when using scanf, since the arrow operator (->) is used to return a struct field value by using the address of that struct variable and it's not used to return the struct field address itself. But if I try to use something like dpp.field it still doesn't work, what is confusing since it supposed to mean something like &dp[index].field , right? I still doesn't figured out how to solve this problem...

After dpp = &dp[i]; is executed, dpp points to an instance of the struct dadosPessoais . Then dpp->cep refers to the cep member of that struct , and &dpp->cep is the address of that member. So, to pass the address of the cep member to scanf , pass &dpp->cep .

When using scanf with %s , you should pass the address of the first character in the array. So, for dpp->nome , you could pass &dpp->nome[0] .

However, since nome is an array, it will be automatically converted to a pointer to its first element when you use it in an expression. 1 So, you can use pass dpp->nome to scanf , and it will be the same as passing &dpp->nome[0] .

Footnote

1 This automatic conversion does not occur when the array is the operand of sizeof , is the operand of the unary & operator (which takes the address), or is a string literal used to initialize an array. Note in particular that &array and array are pointers, but the type of &array is “pointer to array,” whereas array , after automatic conversion, is a pointer to an element of the array.

The proper way to do it should be scanf("%s", dpp->nome). Example:

#include <stdio.h>

struct test {
     char a[10];
};

int main() {
     struct test new_struct_array[1];
     struct test * ptr = &new_struct_array[0];
     scanf("%s", ptr->a);
     printf("%s", ptr->a);
     return 0;
}

In C, arrays will point to the memory address of their first element. That is why when you do something like this:

char nome[10];
scanf("%s", nome);  // works
scanf("%s", &nome); // does not

nome is already a reference to the start of the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM