2

Why am I getting this error?:

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

in this lines:

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

Error:

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

Code

//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;
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571

3 Answers3

0

it has to do with this:

**head

then you do this

*head->next

you are still pointing to the pointer that points to head.

here is complete code:

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;
    }
}

your function should simply be

void insertNode(Node *head, int data)
  • What do you mean? And how? –  Oct 28 '15 at 05:16
  • Last question, @GRC: in C, when do I have to use dereferencing, i.e. `**` ? –  Oct 28 '15 at 05:31
  • Double pointers are headaches for many in C I am not exception :) Maybe this will help you a bit http://stackoverflow.com/questions/5580761/why-use-double-pointer-or-why-use-pointers-to-pointers –  Oct 28 '15 at 06:18
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;
}

Your code had some problems first of: head is already a pointer node that should point to the first node as shown below


|head|-->firstNode-->secondNode, and so on

But, from the code fragment given below

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


I get to see something else happening, you are trying to store the newNode's address to the address of the head**(not the address that the head stores). The address of the head is actually stored by no variable. **This could be the major reason that you're getting the error
Hopefully, I have corrected the code from its syntactical errors yet the methods possibly have some undefined reference. May be you have defined the method somewhere. Hope you have understood the answer

Thankyou

Aniruddha Sinha
  • 799
  • 1
  • 10
  • 22
0

The problem is with the expression *head->next. Since -> has higher precedence than *, this is equivalent to *(head->next). What you want is (*head)->next, so you need the parentheses.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226