简体   繁体   English

在C中打印节点时出错?

[英]Error while printing Nodes in C?

I am trying to print what I have in nodes but it says the following error 我正在尝试打印节点中的内容,但显示以下错误

main.c: In function 'main':
main.c:83:37: error: request for member 'emails' in something not a structure or union
printf("%s\n", tmpNodesUnique[l].emails);
^

I get the above error after I run the following code. 运行以下代码后,出现上述错误。 What am I doing wrong here? 我在这里做错了什么?

Node *tmpNodesUnique[nodesCount];
    int uniqueFound = 0;
    tmpNodesUnique[0] = &tmpNodes[0];
    for (k=1; k<10; k++){
        if (strcmp(tmpNodesUnique[uniqueFound]->emails, tmpNodes[k].emails) != 0){
            tmpNodesUnique[++uniqueFound] = &tmpNodes[k];
        }
    }

    for (k=0; k<=uniqueFound; k++){
      tmpNodesUnique[k]->emails;
    }
    for(l = 0; l <= nodesCount; l++){
            printf("%s\n", tmpNodesUnique[l]->emails);
        }

Probably your struct Node is defined as 可能您的结构Node定义为

typedef struct {
    char *emails;
} Node;

This means if you want to print the member emails you will have to use . 这意味着如果要打印成员emails ,则必须使用. operator and not the -> operator 运算符,而不是->运算符

for(l = 0; l <= nodesCount; l++){
   printf("%s\n", tmpNodesUnique[l].emails);
}

And as pointed out in the comments the following line does nothing, it is an incomplete statement 并且正如注释中指出的那样,以下行不执行任何操作,这是不完整的语句

for (k=0; k<=uniqueFound; k++){
  tmpNodesUnique[k]->emails;
}

Additionally make sure you really want k<= instead of k< it looks suspicious as well 此外,请确保您确实想要k<=而不是k<它看起来也很可疑

It looks like you're not compiling the code you expect. 看来您没有在编译所需的代码。

The error uses a . 错误使用. member operator for emails. 电子邮件的成员运算符。 While you have a -> pointer operator in the code you show. 当您在显示的代码中使用->指针运算符时。 So the code is different (and the error is related to the dot as it's stating that it expects a structure or union member while you've clearly declared a pointer). 因此,代码是不同的(错误与点有关,因为在您明确声明了指针后,它表示需要结构或联合成员)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM