C 使用指针和函数存储和检索数据的结构

C 使用指针和函数存储和检索数据的结构,c,C,大家好,我是c语言的新手,我设法对结构上的数据进行硬编码,并打印它们各自的值。目前,我正在使用指针存储各自的数据,并使用函数打印数据。有人能帮我解释为什么我不能存储和检索数据吗?我不需要答案,只需要逻辑流程就足够了。您的下一个客户功能需要进行以下更改,您可以使用账户直接访问名字和姓氏,但它们在名字中 所以你必须使用acct->names.firstName和acct->names.lastName struct account { struct //A structure inside

大家好,我是c语言的新手,我设法对结构上的数据进行硬编码,并打印它们各自的值。目前,我正在使用指针存储各自的数据,并使用函数打印数据。有人能帮我解释为什么我不能存储和检索数据吗?我不需要答案,只需要逻辑流程就足够了。

您的
下一个客户
功能需要进行以下更改,您可以使用
账户
直接访问
名字
姓氏
,但它们在
名字

所以你必须使用
acct->names.firstName
acct->names.lastName

struct account
{
    struct //A structure inside a structure
    {
        char lastName[10];
        char firstName[10];
    } names; //structure is named as 'name'
    int accountNum;
    double balance;
};

int main()
{
    struct account record;
    int flag = 0;
    do
    {
        nextCustomer(&record);
        if ((strcmp(record.names.firstName, "End") == 0) && //only when the first name entered as "End"
                (strcmp(record.names.lastName, "Customer") == 0)) //and last name entered as "Customer", the loop stops
            flag = 1;
        if (flag != 1)
            printCustomer(record);
    }
    while (flag != 1);
}
void nextCustomer(struct account *acct)
{
    printf("Enter names (firstName lastName):\n"); 
    //scanf("%s, %s", acct->firstName, acct->lastName); //have no idea why first and last name cant be found although im aware thats its a structure inside a structure
    printf("Enter account number:\n");
    //scanf("%d", acct->accountNum); 
    printf("Enter balance:\n");
    scanf("%f", acct->balance);
}
void printCustomer(struct account acct)
{
    printf("Customer record: \n");
    printf("%d", acct.accountNum); //can't seem to retrieve data from the strcture
}

您在哪里分配内存来存储数据?我看到记录一次又一次地被覆盖。我认为您应该使用帐户数组。所谓存储,是指将其存储在内存中还是磁盘上的文件中?在启用编译器警告的情况下重建代码;消息将导致各种丢失的
&
@Merlina,如果这真的解决了您的问题或有助于解决您的问题,请接受答案。
void nextCustomer(struct account *acct)
{
    printf("Enter names (firstName lastName):\n"); 
    scanf("%s%s", acct->names.firstName, acct->names.lastName); //have no idea why first and last name cant be found although im aware thats its a structure inside a structure
    printf("Enter account number:\n");
    scanf("%d", &acct->accountNum); 
    printf("Enter balance:\n");
    scanf("%lf", &acct->balance);
}

void printCustomer(struct account acct)
{
    printf("Customer record: \n");
    printf("F: %s\n", acct.names.firstName);
    printf("L: %s\n", acct.names.lastName);
    printf("A: %d\n", acct.accountNum); //can't seem to retrieve data from the strcture
    printf("B: %lf\n", acct.balance);
}