1 Replies - 588 Views - Last Post: 17 February 2021 - 06:53 AM Rate Topic: -----

#1 mircea_dragu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-February 19

C Programming linked List

Posted 17 February 2021 - 03:51 AM

Hi,
I have a problem regarding linked list, namely, i want to create a linked list in the same way. It is important for me to keep the prototype of function as it is. Thank you.
When i compile i receive the next error : unknown type name "node".

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct node
{
	int data;
	struct node *next;
};

struct node *head;

int sorted_list_insert(head , newNode);

int main()
{
    struct node *newNode;
    int data;
    sorted_list_insert(*head , newNode);
}

int sorted_list_insert(node **head , node *newNode)
{
    char dataString[30];
    newNode = (struct node *)malloc(sizeof(node));
    if(newNode == NULL)
    {
        printf("\n Unable to allocate memory! \n");
    }
    else
    {
        printf("\n Enter a string for the first node : ");
        scanf("%d", & data);
    }

}


Is This A Good Question/Topic? 0
  • +

Replies To: C Programming linked List

#2 jimblumberg   User is online

  • member icon

Reputation: 5910
  • View blog
  • Posts: 17,925
  • Joined: 25-December 09

Re: C Programming linked List

Posted 17 February 2021 - 06:53 AM

Why the global variables? You seem to be passing the variables as parameters so there is no need for those globals.


Since this is C you must either use a typedef or the keyword struct when referring to structures. Also you always need to provide the type for the prarameters in a function prototype, and the function prototype must match the function definition.

You should also not be casting the return value from malloc, that is considered a bad practice in C since the cast can mask serious problems.

Lastly, for now, do you realize that that newNode pointer is being passed by value? Which means that any change to the pointer will be local to the function, and you promised to return a value, so you must return the promised value.

In future show us your compile errors/warnings, all of them, exactly as they appear in your development system. Those warnings have important information embedded within them to aid in locating and fixing the problems.

It also appears to me that you waited a little long before you actually compiled the program since you have quite a few problems with the code. You should be compiling early, I usually start compiling after creating a "blank" main(), then again after as little as one line of code. The more often you compile the fewer errors/warnings you will receive and it will make finding the problems easier because you should only need to look at a few lines of code for the problems. Oh and never ignore warnings, consider them as errors and fix them as soon as they are discovered.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1