简体   繁体   中英

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

typedef struct {
    char *emails;
} Node;

This means if you want to print the member emails you will have to use . 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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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