2017-03-01 42 views
2

これは初めてのC言語ですが、穏やかではありません。私は他の言語で私に馴染みのあることをしようとしていますが、このポインタの問題は私を激しく襲っています。- >を使用して構造体のプロパティにアクセスしましたが、使用するようにコンパイラエラーが発生しました - >?

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 **, ...)これは私が**に出くわした最初のものなので、どうすればいいのか分かりません。私はそれがダブルポインタ(ポインタへのポインタですか?)だと思います。

渡された引数のプロパティを変更するにはどうすればよいですか?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'がnullで、逆参照すると、時間が掛かります。 – aschepler

+2

自分自身で、 'struct record * first = * firstRecord'というローカル変数を作成してください。それで 'first-> name'が有効になります。 –

+0

よろしくお願いします。 – kneeki

答えて

4

firstRecord->nameは、(*firstRecord).nameに相当します。それはポインタを逆参照し、次にメンバを探します。

しかし、firstRecordは、structへのポインタではなく、structへのポインタへのポインタです。したがって、2回参照を解除する必要があります。 (**firstRecord).nameまたは(*firstRecord)->nameのいずれかを使用します。

+0

説明をありがとう! – kneeki

+0

この状況でコンパイラのメッセージが悪いことに注意してください。バグレポートを提出しなければなりません。 –

関連する問題