使用->访问结构的属性,但得到编译器错误告诉我使用-&gt ;? [英] Using -> to access property of struct but getting compiler error telling me to use ->?

查看:76
本文介绍了使用->访问结构的属性,但得到编译器错误告诉我使用-&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用C,不是很温柔.我正在尝试用其他语言做一些我熟悉的事情,但是这个指针问题使我很难受.

This is my first time using C, it's not gentle. I'm trying to do something familiar to me in other languages, but this pointer issue is hitting me hard.

我收到错误消息:

recordFunctions.c:178:20: error: ‘*firstRecord’ is a pointer; did you mean to use ‘->’?
     firstRecord->accountno = 1;
                ^~
                ->
recordFunctions.c:179:27: error: ‘*firstRecord’ is a pointer; did you mean to use ‘->’?
     strcpy(firstRecord->name, name);
                       ^~
                       ->
recordFunctions.c:180:27: error: ‘*firstRecord’ is a pointer; did you mean to use ‘->’?
     strcpy(firstRecord->address, address);
                       ^~
                       ->
recordFunctions.c:181:20: error: ‘*firstRecord’ is a pointer; did you mean to use ‘->’?
     firstRecord->next = NULL;
                ^~
                ->
recordFunctions.c:202:32: error: ‘*iterator’ is a pointer; did you mean to use ‘->’?
         iterator = iterator->next;
                            ^~
                            ->

我觉得这让我特别困惑.该函数的参数由教授提供,无法更改,因此我需要向该函数传递一个(双指针?). int addRecord(struct record **, ...)这是我第一次遇到**,所以我不确定该怎么做.我相信这是一个双指针(指向指针的指针?).

Which I find to be especially confusing to me. The parameters of the function are provided by the professor, and cannot be changed, so I'm needing to pass a (double pointer?) into the function. int addRecord(struct record **, ...) This is the first I've come across ** so I'm not sure what to do with it. I believe it's a double pointer (a pointer to a pointer?).

我该如何修改传递的参数firstPointer的属性?

How would I go about modifying the properties of the passed argument firstPointer?

/*
 * Function Name:   addRecord
 *
 * Description:     Adds a record to the database.
 *
 * Parameters:      next (record **)
 *                  accountno (int)
 *                  name (char[])
 *                  address (char[])
 *
 * Return Values:   1: success
 *                  0: fail
 */
int addRecord (struct record ** firstRecord, int accountno, char name[], char address[])
{
    if (debugmode == 1)
    {
        printf("\n========================================================================================================");
        printf("\n*** addRecord(struct record **, int, char [], char []): Parameters passed:\n");
        printf("%20s %20s %20s %20s %20s\n", "Address", "Name", "Datatype", "Scope", "Value");
        printf("%20p %20s %20s %20s %20s\n", (char *) &firstRecord, "firstRecord", "pointer", "addRecord", "");
        printf("%20p %20s %20s %20s %20d\n", (void *) &accountno, "accountno", "int", "addRecord", accountno);
        printf("%20p %20s %20s %20s %20s\n", (void *) &name, "name", "char[25]", "addRecord", name);
        printf("%20p %20s %20s %20s %20s\n", (void *) &address, "address", "char[80]", "addRecord", address);
        printf("========================================================================================================\n");
    }

    // Check if firstRecord is NULL, if so, this is the firstRecord otherwise
    // create a new record and add it to the end of the database
    if (firstRecord == NULL)
    {
        firstRecord->accountno = 1;
        strcpy(firstRecord->name, name);
        strcpy(firstRecord->address, address);
        firstRecord->next = NULL;
    }
    else
    {
        // Define a new int called totalRecords
        int totalRecords = 0;
        // Define a new structure pointer called <newRecord>
        struct record * newRecord;
        // Allocate space for the new record on the heap
        newRecord = NULL;
        // Assign values to newRecord properties
        strcpy(newRecord->name, name);
        strcpy(newRecord->address, address);
        // Iterate through the records until we reach the end
        struct record ** iterator = firstRecord;
        // Start iterating through the records
        while (iterator != NULL)
        {
            // Increment totalRecords by 1
            totalRecords++;
            // Advance to the next record
            iterator = iterator->next;
        }
        // Increment totalRecords by one while assigning it to the
        // accountno property.
        newRecord->accountno = totalRecords++;
    }

    return 1;
}

推荐答案

firstRecord->name等同于(*firstRecord).name.它取消引用指针,然后寻找成员.

firstRecord->name is equivalent to (*firstRecord).name. It dereferences a pointer and then looks for a member.

但是firstRecord是指向struct的指针,而不是struct的指针,因此您需要取消引用两次.使用(**firstRecord).name(*firstRecord)->name之一.

But firstRecord is a pointer to pointer to struct, not a pointer to struct, so you need to dereference twice. Use either of (**firstRecord).name or (*firstRecord)->name.

这篇关于使用-&gt;访问结构的属性,但得到编译器错误告诉我使用-&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆