簡體   English   中英

C中的“非法使用選擇器”

[英]“Illegal use of selector” in C

作為較大項目的一部分,我試圖編寫一個C函數,該函數在已實現的排序鏈表中搜索struct olnode的值。 但是,我收到一些錯誤。 我是C語言的新手,我在指針和雙指針以及何時使用什么方面都在掙扎,所以我認為這是問題的一部分,但我不確定如何解決該問題。 包括所有必需的標題。 這是在使用cc作為編譯器的Minix 2.0.4上。

我可以提供任何其他必要的代碼; 因為我不熟悉C,所以我不確定我需要顯示多少內容,因此我提供的是我認為需要的內容,僅此而已。

全局代碼(標頭除外):

#define POOLSZ 53
struct olnode {
    int eventnr;
    int eventfq;
    struct olnode *next;
};
typedef struct olnode olnode;
olnode pool[POOLSZ];
olnode *avail;    /* points to first available node */

返回錯誤的函數(完成后搜索傳遞的int, *current應該是保存當前值的olnode ):

void
srchfreq(olnode *list, int xfrequency, olnode **current)
{
    olnode *previous, *newnext;

    while(current->eventfq > xfrequency) {
        *previous = &current;
        *newnext = current->next;
        *current = *newnext;
    }
}

srchfreq()函數調用(在另一個函數中):

/* *list points to the first node in the list
   (*current).eventfq is the value being searched for
*/

srchfreq(*list, (*current).eventfq, &current);

錯誤(行號被編輯為相對於srchfreq()行,如上所述):

line 6: illegal use of selector eventfq
line 7: cannot convert pointer to struct
line 8: illegal use of selector next
line 8: cannot convert pointer to struct
line 9: cannot convert struct to pointer
void
srchfreq(olnode *list, int xfrequency, olnode **current)
{
    olnode *previous, *newnext;

    while((*current)->eventfq > xfrequency) {
        previous = *current;
        newnext = (*current)->next;
        *current = newnext;
    }
}

第二部分取決於參數的類型。 如果list聲明為olnode *list ,則無需取消引用它,因為函數需要一個指針。 第二個和第三個參數是錯誤的(其中一個-確定我們需要知道current的聲明方式)。

current類型為olnode** ,或指向oldnode的指針。 要獲得指向olnode的指針,請取消引用該指針一次:

*current;

要獲取olnode本身,請從將指針解引用到指針來解引用指針

**current;

因此,在您的情況下,獲取字段eventfq

(**current).eventfq

C還提供了一種快捷方式,其中操作(*ptr).field prt->fieldprt->field完全等效。

在您的情況下,您可以將其應用於

(*current)->eventfq

錯誤,按出現順序:

  • 由於current是指向olnode 的指針 ,因此它不能直接引用任何字段。 但是*current可以。
  • *previousolnode&current是指向olnode的指針。
  • 看到第一個錯誤
  • *newnext是一個olnode
  • *current是指向olnode的指針

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM