C - 请求在非结构或工会中的成员"下一个"



为什么会出现此错误?:

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_ProjectsLab9main.c||In function 'insertNode':|
C:Users\C_ProjectsLab9main.c|38|error: request for member 'next' in something not a structure or union|
C:UserssC_ProjectsLab9main.c|39|error: request for member 'next' in something not a structure or union|
C:Users\C_ProjectsLab9main.c|44|error: request for member 'next' in something not a structure or union|
C:Users\C_ProjectsLab9main.c|48|warning: assignment from incompatible pointer type [enabled by default]|
C:Users\C_ProjectsLab9main.c|50|warning: assignment from incompatible pointer type [enabled by default]|
C:Users\C_ProjectsLab9main.c|51|warning: assignment from incompatible pointer type [enabled by default]|
C:Users\C_ProjectsLab9main.c|56|warning: return type defaults to 'int' [-Wreturn-type]|
C:Users\C_ProjectsLab9main.c||In function 'printList':|
C:Users\C_ProjectsLab9main.c|58|error: request for member 'next' in something not a structure or union|
C:Users\C_ProjectsLab9main.c|63|error: request for member 'next' in something not a structure or union|
C:Users\C_ProjectsLab9main.c|65|warning: implicit declaration of function 'print' [-Wimplicit-function-declaration]|
C:Users\C_ProjectsLab9main.c|65|error: expected ')' before 'current'|
C:Users\C_ProjectsLab9main.c|66|warning: assignment from incompatible pointer type [enabled by default]|
C:Usersshohi_000C_ProjectsLab9main.c|72|warning: return type defaults to 'int' [-Wreturn-type]|
C:Users\C_ProjectsLab9main.c||In function 'deleteList':|
C:Users\C_ProjectsLab9main.c|74|error: request for member 'next' in something not a structure or union|
C:Users\C_ProjectsLab9main.c|74|error: expected statement before ')' token|
C:Users\C_ProjectsLab9main.c|79|error: 'else' without a previous 'if'|
C:Users\C_ProjectsLab9main.c|80|error: request for member 'next' in something not a structure or union|
C:Users\C_ProjectsLab9main.c|83|warning: assignment from incompatible pointer type [enabled by default]|
C:Users\C_ProjectsLab9main.c||In function 'main':|
C:Users\C_ProjectsLab9main.c|98|warning: initialization makes integer from pointer without a cast [enabled by default]|
C:Users\C_ProjectsLab9main.c|99|warning: initialization makes integer from pointer without a cast [enabled by default]|
C:Users\C_ProjectsLab9main.c|101|warning: initialization makes integer from pointer without a cast [enabled by default]|
C:Users\C_ProjectsLab9main.c|101|warning: unused variable 'maxValue' [-Wunused-variable]|
C:Users\C_ProjectsLab9main.c|98|warning: unused variable 'seedNum' [-Wunused-variable]|
C:Users\C_ProjectsLab9main.c||In function 'printList':|
C:Users\C_ProjectsLab9main.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;
}
#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;
}

您的代码出现了一些问题第一个:head已经是一个指针节点,应该指向第一个节点,如下所示


|head|-->firstNode-->secondNode,依此类推

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

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


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

谢谢

这与有关

**head

然后你做这个

*head->next

你仍然指向指向头的指针。

这是完整的代码:

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)

问题出在表达式*head->next上。由于->具有比*更高的优先级,因此这相当于*(head->next)。你想要的是(*head)->next,所以你需要括号。

相关内容

  • 没有找到相关文章

最新更新