kneeki kneeki -4 years ago 104
C Question

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

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download