'的操作数*';必须是c中的指针错误

'的操作数*';必须是c中的指针错误,c,struct,malloc,nodes,C,Struct,Malloc,Nodes,我正在尝试使用结构节点和链表进行赋值。下面是代码的一小部分,在这里我遇到了一个无法解决的错误。请帮忙,谢谢 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> struct term{ char term[200]; double value; }; void main(){ struct term **terms =

我正在尝试使用结构节点和链表进行赋值。下面是代码的一小部分,在这里我遇到了一个无法解决的错误。请帮忙,谢谢

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct term{
    char term[200];
    double value;
};

void main(){
    struct term **terms = (struct term *)malloc(sizeof(struct term)); 
    // Using **terms is mandatory for the project
    *terms[0]->term = "abc";
    *terms[0]->value = 1; //This is line 15
    //I am unable to fix the error in line 15 at "*terms". 
    //The error message states "Struct term **terms: Operend of '*' must be a pointer"
    //The goal is to create nodes that can be accessed using *terms[i] with a for loop of index i
}
#包括
#包括
#包括
#包括
结构术语{
字符项[200];
双重价值;
};
void main(){
结构术语**术语=(结构术语*)malloc(sizeof(结构术语));
//项目必须使用**术语
*术语[0]->term=“abc”;
*术语[0]->value=1;//这是第15行
//我无法修复第15行“*术语”中的错误。
//错误消息声明“Struct term**terms:Operend of'*'必须是指针”
//目标是创建可以使用带有索引i的for循环的*terms[i]访问的节点
}

变量“terms”是一个**(指向指针的指针),但要取消引用它三次:一次使用*,一次使用[],一次使用->。您可能指的是“术语[0]->term”。

此实现存在一些问题。首先,您要创建一个双指针,因此还需要为指向的内部指针分配内存

struct term** terms = (struct term**)malloc(sizeof(struct term*));

*terms = (struct term*)malloc(sizeof(struct term));
其次,您不能执行如下所示的实现

char c[5];
void main(void)
{
    c = "abc";
}
这是错误的,编译器将抛出错误。您可以做的是将每个字符逐个分配给char数组。或者,您也可以使用strcpy

((*terms)->term)[0] = 'a';
((*terms)->term)[1] = 'b';
((*terms)->term)[2] = 'c';
鉴于此

terms
表示类型为
struct term**
的对象,是指向
struct term
的指针。那么,

  • *terms[0]->term
    在运算符优先级上等同于
    *((terms[0])->term)
  • terms[0]
    相当于
    *terms
    。它是类型为
    struct term*
    的指针
  • terms[0]->term
    指定
    terms[0]
    指向的结构的成员
    term
    ,它是一个数组。为了计算较大的表达式,此数组值将自动转换为指向第一个数组元素的指针
  • 因此,
    *terms[0]->term
    表示数组中的第一个字符
    terms[0]->term
    。它相当于
    terms[0]->term[0]
    。尝试为这一个字符分配字符串文字是没有意义的
类似地,
terms[0]->value
指定
terms[0]
指向的结构的成员
value
。这不是指针,因此它不是
*
运算符的合适操作数。这是编译器的错误消息告诉您的

从语法上讲,这些术语中的任何一个都与
术语的类型一致:

  • 术语[0]->值=1
  • (*术语[0])。值=1
  • (*术语)->值=1
然而,即使使用其中的一个,代码仍然会被严重破坏。我已经提到了
*terms[0]的错误赋值。term
。另外,
malloc()
调用为要指向的
terms
保留了错误的内存量,尽管对于单个
struct term*
来说可能已经足够了。然而,最有问题的是,
术语[0]
本身,即指针,从未使用有效(指针)值初始化。它没有指向任何您可以实际分配给其成员的对象。那么,你可能想要更像这样的东西:

    struct term **terms = malloc(sizeof(*terms)); // enough space for one pointer
    *terms = malloc(sizeof **terms));             // enough space for one struct term

    strcpy(terms[0]->term, "abc");
    terms[0]->value = 1;
    // ...

另外,
main()
的返回类型必须是
int
,而不是
void

“terms”是一个**,您要取消引用它三次:一次使用*,一次使用[],一次使用->。您的意思可能是“术语[0]->术语”。我们通常会快速回答,但我们不会按照您的时间表工作,因此我们通常不会善待快速响应的请求。非常感谢!它解决了问题。第二句可以,第一句不行
*
不解除对
术语的引用或对其的直接表达,而是解除对
术语
成员的引用
    struct term **terms = malloc(sizeof(*terms)); // enough space for one pointer
    *terms = malloc(sizeof **terms));             // enough space for one struct term

    strcpy(terms[0]->term, "abc");
    terms[0]->value = 1;
    // ...