"Illegal use of selector" 在 C 中
"Illegal use of selector" in C
作为一个更大项目的一部分,我正在尝试编写一个 C 函数,该函数在已实现的排序链表中搜索结构 olnode
中的值。但是,我收到了一些错误。我是 C 的新手,我正在为指针和双指针以及何时使用什么而苦苦挣扎,所以我认为这是问题的一部分,但我不确定如何解决问题。所有必要的 headers 都包括在内。这是在 Minix 2.0.4 上使用 cc 作为编译器。
我可以提供任何额外的必要代码;因为我不熟悉 C,所以我不确定我需要展示多少,所以我提供了我认为需要的东西,仅此而已。
全球代码(headers除外):
#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 = ¤t;
*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, ¤t);
错误(行号被编辑为相对于上面给出的 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**
,或指向旧节点的指针。要获取指向 olnode 的指针,请取消引用该指针一次:
*current;
要获取 olnode 本身,请取消引用您通过取消引用指针获得的指针
**current;
所以在你的情况下,抢占字段eventfq
(**current).eventfq
C 还提供了一个快捷方式,其中操作 (*ptr).field
完全等同于 prt->field
.
在您的情况下,您可以将此应用于
(*current)->eventfq
错误,按出现顺序排列:
- 因为
current
是一个指向指向olnode
的指针,它不能直接引用任何字段;但是 *current
可以。
*previous
是一个 olnode
,¤t
是一个指向指向 olnode
. 的指针的指针
- 看到第一个错误
*newnext
是一个 olnode
*current
是指向 olnode
的指针
作为一个更大项目的一部分,我正在尝试编写一个 C 函数,该函数在已实现的排序链表中搜索结构 olnode
中的值。但是,我收到了一些错误。我是 C 的新手,我正在为指针和双指针以及何时使用什么而苦苦挣扎,所以我认为这是问题的一部分,但我不确定如何解决问题。所有必要的 headers 都包括在内。这是在 Minix 2.0.4 上使用 cc 作为编译器。
我可以提供任何额外的必要代码;因为我不熟悉 C,所以我不确定我需要展示多少,所以我提供了我认为需要的东西,仅此而已。
全球代码(headers除外):
#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 = ¤t;
*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, ¤t);
错误(行号被编辑为相对于上面给出的 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**
,或指向旧节点的指针。要获取指向 olnode 的指针,请取消引用该指针一次:
*current;
要获取 olnode 本身,请取消引用您通过取消引用指针获得的指针
**current;
所以在你的情况下,抢占字段eventfq
(**current).eventfq
C 还提供了一个快捷方式,其中操作 (*ptr).field
完全等同于 prt->field
.
在您的情况下,您可以将此应用于
(*current)->eventfq
错误,按出现顺序排列:
- 因为
current
是一个指向指向olnode
的指针,它不能直接引用任何字段;但是*current
可以。 *previous
是一个olnode
,¤t
是一个指向指向olnode
. 的指针的指针
- 看到第一个错误
*newnext
是一个olnode
*current
是指向olnode
的指针