Simple linked list-C

After taking the summer off from C programming, I'm back in the thick of it for classes and am trying to catch up, particularly in pointers.

The current assignment has us converting a program from an array structure to a simple linked list. In order to refresh my memory, I've tried implementing it in a standalone program, but am running into trouble.

My code:

struct node{ int val; struct node *next; }; typedef struct node *item; item newNode(void); //function prototype void main(){ item *cur, *itemList; int i; itemList=NULL; for (i=0; i<=10; i++){ cur= newNode(); cur->val=i; cur->next= itemList; } } item newNode(void) { item box; /* the new object to return */ box = (item) malloc (sizeof (struct node)); if (box == NULL) { printf("ERROR: emalloc failed for new Box\n"); exit(0); } /* initialize fields */ box->val=0; return box; }

The first error message is coming at cur= newBox() and is stating that an assigment from an incompatible pointer type is being made. I'm not sure why, since cur is a pointer to a node, and box is a structure. Where does the incompatible pointer come from?


The first problem is that your doing item *cur, *itemList; which is a node**. Change that to item cur, itemList; to get node*'s; you don't need a pointer to a pointer to a node, just a pointer to a node.

Another problem is that you're setting all the next pointers of your nodes to itemList without setting itemList to cur at the end of each loop iteration (which will make itemList point to the beginning of the list at the end of the loop).


You need a pointer

It is clearer if your typedef is like this:

typedef struct node item;


item *newNode(void) { item *box; /* the new object to return */ box = (item) malloc (sizeof (struct node)); if (box == NULL) { printf("ERROR: emalloc failed for new Box\n"); exit(0); } /* initialize fields */ box->val=0; return box; }

Also you call the function newNode and newBox in different places.

You also need to reset the head pointer:

for (i=0; i<=10; i++){ cur= newBox(); cur->val=i; cur->next= itemList; itemList = cur; }


In main you are using item* which is node**. Just remove the * in the declaration list in main.


cur is of type item*, pointer to item. But the return type of newNode(void) is item. They both are not type compatible.


  • Circular Queue Python
  • pointer is not modifying in insert in c
  • appending a new node to a nodelist using foreach loop and XmlNodeList C#
  • code behaviour is strange
  • Passing unspecialized template as a template parameter
  • Multiple sockets for clients to connect to
  • Complex trait requirements on struct
  • iOS - Is this a task for enums?
  • Build Matrix of Comparisons in SQl Server
  • Accessing the variables from a PHP Anonymous Function
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • Monotouch crashes with NullReferenceException on non nullable object
  • sweetalert2 inputoptions from file in select example
  • C++ Single function pointer for all template instances
  • In loopback documentation what does variable 'cb' stands for?
  • Group list of tuples by item
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • CakePHP ACL tutorial initDB function warnings
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • jQuery .attr() and value
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Can Jackson SerializationFeature be overridden per field or class?
  • ActionScript 2 vs ActionScript 3 performance
  • Large data - storage and query
  • How can I estimate amount of memory left with calling System.gc()?
  • Apache 2.4 - remove | delete | uninstall
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Acquiring multiple attributes from .xml file in c#
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • apache spark aggregate function using min value
  • How can I remove ASP.NET Designer.cs files?
  • Sorting a 2D array using the second column C++
  • How can i traverse a binary tree from right to left in java?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?