Using -> to access property of struct but getting compiler error telling me to use ->?

元气小坏坏 提交于 2020-01-11 10:32:23

问题


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.

I'm given the error message:

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;
                            ^~
                            ->

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?).

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;
}

回答1:


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

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.



来源:https://stackoverflow.com/questions/42521864/using-to-access-property-of-struct-but-getting-compiler-error-telling-me-to-u

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!