在非结构或联合中请求成员“'next'”

request for member `'next'` in something not a structure or union

为什么会出现此错误?:

request for member 'next' in something not a structure or union|

这几行:

 if(*head->next == NULL){
        *head->next = newNode;

错误:

||=== Build: Debug in Lab9 (compiler: GNU GCC Compiler) ===|
C:\Users\C_Projects\Lab9\main.c||In function 'insertNode':|
C:\Users\C_Projects\Lab9\main.c|38|error: request for member 'next' in something not a structure or union|
C:\Users\s\C_Projects\Lab9\main.c|39|error: request for member 'next' in something not a structure or union|
C:\Users\C_Projects\Lab9\main.c|44|error: request for member 'next' in something not a structure or union|
C:\Users\C_Projects\Lab9\main.c|48|warning: assignment from incompatible pointer type [enabled by default]|
C:\Users\C_Projects\Lab9\main.c|50|warning: assignment from incompatible pointer type [enabled by default]|
C:\Users\C_Projects\Lab9\main.c|51|warning: assignment from incompatible pointer type [enabled by default]|
C:\Users\C_Projects\Lab9\main.c|56|warning: return type defaults to 'int' [-Wreturn-type]|
C:\Users\C_Projects\Lab9\main.c||In function 'printList':|
C:\Users\C_Projects\Lab9\main.c|58|error: request for member 'next' in something not a structure or union|
C:\Users\C_Projects\Lab9\main.c|63|error: request for member 'next' in something not a structure or union|
C:\Users\C_Projects\Lab9\main.c|65|warning: implicit declaration of function 'print' [-Wimplicit-function-declaration]|
C:\Users\C_Projects\Lab9\main.c|65|error: expected ')' before 'current'|
C:\Users\C_Projects\Lab9\main.c|66|warning: assignment from incompatible pointer type [enabled by default]|
C:\Users\shohi_000\C_Projects\Lab9\main.c|72|warning: return type defaults to 'int' [-Wreturn-type]|
C:\Users\C_Projects\Lab9\main.c||In function 'deleteList':|
C:\Users\C_Projects\Lab9\main.c|74|error: request for member 'next' in something not a structure or union|
C:\Users\C_Projects\Lab9\main.c|74|error: expected statement before ')' token|
C:\Users\C_Projects\Lab9\main.c|79|error: 'else' without a previous 'if'|
C:\Users\C_Projects\Lab9\main.c|80|error: request for member 'next' in something not a structure or union|
C:\Users\C_Projects\Lab9\main.c|83|warning: assignment from incompatible pointer type [enabled by default]|
C:\Users\C_Projects\Lab9\main.c||In function 'main':|
C:\Users\C_Projects\Lab9\main.c|98|warning: initialization makes integer from pointer without a cast [enabled by default]|
C:\Users\C_Projects\Lab9\main.c|99|warning: initialization makes integer from pointer without a cast [enabled by default]|
C:\Users\C_Projects\Lab9\main.c|101|warning: initialization makes integer from pointer without a cast [enabled by default]|
C:\Users\C_Projects\Lab9\main.c|101|warning: unused variable 'maxValue' [-Wunused-variable]|
C:\Users\C_Projects\Lab9\main.c|98|warning: unused variable 'seedNum' [-Wunused-variable]|
C:\Users\C_Projects\Lab9\main.c||In function 'printList':|
C:\Users\C_Projects\Lab9\main.c|69|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 10 error(s), 14 warning(s) (0 minute(s), 0 second(s)) ===|

代码

//Defines the struct of Node
typedef struct NodeStruct{
    int data;
    struct Node *next;
}Node;
//The function insert a newly created Node into its proper position
//The list will be sorted in ascending order
void insertNode(Node **head, int data){
    //Create a new Node with the given num
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->data = data;

    //Check if the list is empty
    if(*head->next == NULL){
        *head->next = newNode; //point head's next to the newNode
        newNode->next = NULL;
    }
    //If the list is not empty, then insert the newNode into its proper position
    else{
        Node *current = *head->next; //starts from the very first element(after dummy node!)
        Node *previous = NULL; //previous is needed for storing the previous node before exiting the loop
        while(current != NULL && current->data < data){
            previous = current;
            current = current->next;
        }
        previous->next = newNode; //place the node between previous and current
        newNode->next = current;
    }
}
int main(int argc, char *argv[])
{
    //If argc < 4 then quit the program
    if(argc < 4){
        badProgram();
    }
    else{
        Node *head = dummyNode();
        int seedNum = argv[1]; //gets the integer for seeding the random generator
        int totalNums = argv[2]; //gets the total number of random numbers
        srand(totalNums); //input into the srand()
        int maxValue = argv[3]; //maximum possible value

        int i;
        for(i = 0; i < totalNums; i++){
            int num = rand(); //generates a random number
            fprintf(stdout, "%d", num); //outputs the number to the screen
            insertNode(&head, num);
        }
    }


    return 0;
}

与此有关:

**head

那你就这样做

*head->next

你还在指向指向head的指针

完整代码如下:

void insertNode(Node *head, int data){
    //Create a new Node with the given num
    Node *newNode = malloc(sizeof(Node));
    newNode->data = data;

    //Check if the list is empty
    if(head->next == NULL){
        head->next = newNode; //point head's next to the newNode
        newNode->next = NULL;
    }
    //If the list is not empty, then insert the newNode into its proper position
    else{
        Node *current = head->next; //starts from the very first element(after dummy node!)
        Node *previous = NULL; //previous is needed for storing the previous node before exiting the loop
        while(current != NULL && current->data < data){
            previous = current;
            current = current->next;
        }
        previous->next = newNode; //place the node between previous and current
        newNode->next = current;
    }
}

你的功能应该只是

void insertNode(Node *head, int data)
#include<stdio.h>
#include<stdlib.h>
typedef struct NodeStruct{
    int data;
    struct Node *next;
}Node;
//The function insert a newly created Node into its proper position
//The list will be sorted in ascending order
void insertNode(Node **head, int data){
    //Create a new Node with the given num
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next=NULL;
    //Check if the list is empty
    if(head == NULL){
        head = newNode; //point head's next to the newNode
       // head->next = NULL;
    }
    //If the list is not empty, then insert the newNode into its proper position
    else{
        Node *current = head; //starts from the very first element(after dummy node!)
        Node *previous = NULL; //previous is needed for storing the previous node before exiting the loop
        while(current != NULL && current->data < data){
            previous = current;
            current = current->next;
        }
        previous->next = newNode; //place the node between previous and current
        newNode->next = current;
    }
}
int main(int argc, char *argv[])
{
    //If argc < 4 then quit the program
    if(argc < 4){
        badProgram();
    }
    else{
        Node *head = dummyNode();
        int seedNum = argv[1]; //gets the integer for seeding the random generator
        int totalNums = argv[2]; //gets the total number of random numbers
        srand(totalNums); //input into the srand()
        int maxValue = argv[3]; //maximum possible value

        int i;
        for(i = 0; i < totalNums; i++){
            int num = rand(); //generates a random number
            fprintf(stdout, "%d", num); //outputs the number to the screen
            insertNode(&head, num);
        }
    }


    return 0;
}

你的代码有一些问题 first of: head 已经是一个指针节点,应该指向第一个节点,如下所示


|head|-->firstNode-->secondNode,等等

但是,从下面给出的代码片段

if(*head->next == NULL){
        *head->next = newNode; //point head's next to the newNode
        newNode->next = NULL;
    }


我看到发生了其他事情,您正在尝试将新节点的地址存储到头部的地址**(不是头部存储的地址)。 head的地址实际上是没有变量存储的。 **这可能是您收到错误的主要原因
希望我已经纠正了代码的语法错误,但这些方法可能有一些未定义的引用。可能是您在某处定义了该方法。 希望你已经明白答案

谢谢

表达式 *head->next 有问题。由于 -> 的优先级高于 *,因此等同于 *(head->next)。你想要的是(*head)->next,所以你需要括号。