• 0

[C++] Linked lists: "invalid conversion from void* to node"


Question

Okay, so I've been kind of putting off learning linked lists for a couple of days because I hate learning things that I don't completely understand... but I finally decided to just jump in and go with it.

I am following a tutorial from Stanford's Computer Science department and have basically copied the tutorial code (except changed 'head' to 'root', just to be different :))

#include <iostream>

using namespace std;

struct node {

	   int data;
	   struct node* next;

};

struct node* buildNodes() {

	   struct node* root = NULL;
	   struct node* second = NULL;
	   struct node* third = NULL;

	   //allocate three nodes in the heap
	   root = malloc(sizeof(struct node));
	   second = malloc(sizeof(struct node));
	   third = malloc(sizeof(struct node)); 

	   root->data = 1;
	   root->next = second;

	   root->data = 2;
	   root->next = third;

	   root->data = 3;
	   root->next = NULL;

	   return root;

}

int main() {

}

Okay, I'm starting to understand this a little more... except I have no idea why my compiler (Bloodshed's Dev-C++) gives me the error "invalid conversion from 'void*' to 'node*'." (it points to the lines using the malloc() function).

hmm.... I could have sworn

struct node* root = NULL;

would make 'root' the type 'node'. I really have very little clue about what I'm saying... heh

any ideas?

5 answers to this question

Recommended Posts

  • 0
  nvme said:
i dont think you need to prefix it with 'struct'. i think it only needs to be:

node* root = NULL;

i could be wrong though.

ah, that works as well. I think the tutorial I found is for both C and C++, and it was also written in 2002 so it may be outdated a bit... Thanks for the help.

  • 0

With C, the 'struct' keyword is required unless you do a typedef. If you use a typedef, you still need to use struct inside the actual struct:

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

Node* root = NULL;

And in C++, NULL is the same as 0, but with less typing required. :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Gemini CLI brings AI smarts to your Firebase terminal experience by Paul Hill Google has added its Gemini AI model directly into the command-line interface of Firebase Studio, its cloud-based IDE that uses AI to help with projects. The Gemini Command Line Interface (CLI) means that developers can expand past using AI for code, they can now also use AI for content generation and research without leaving the IDE. Gemini CLI comes with free usage tiers (60 model requests per minute, 1,000 requests per day with a Google login), it offers advanced AI features, and includes integrated Google Search for real-time content. Gemini CLI is also open source so it can be customized and accepts contributions. Accessing the Gemini CLI within Firebase Studio is straightforward, just press “Code view” in the top-right. From there, open up the terminal from the burger menu then select Terminal and New Terminal. Then in the terminal, type gemini and go through the setup, you can just press enter twice to get started. Out of the box, you’ll be able to get started with Gemini 2.5 Pro by just typing a query and pressing enter. There is also a non-interactive mode that’s useful for scripting and automation. To use it you use the –prompt or -p flags followed by your query wrapped in quotes, for example: gemini -p “What is the capital of France?” In this mode, Gemini CLI automatically closes after completing the request. During setup, there was the option to choose a theme. If you ever want to change it or look at other settings such as usage states, tool access, or checkpointing, you can edit them via .gemini/settings.json. You can also add API keys or choose different models in .env and you can using GEMINI.md to provide project-specific context, instructions, and coding styles in Gemini for a more tailored response. With Gemini CLI, you can have it explain code, refactor code, debug errors, and summarize information. It’s as simple as typing explain [file], refactor , debug “Error message”, or summarize “topic”. There are also built-in commands for managing the session such as /help for a command list, /chat to save and resume conversations, /tools to see available tools, and /restore to undo tool-made file edits. Firebase Studio, in true Google fashion, is a cloud-based IDE used in your web browser, making it excellent for weaker computers. To get started, you can head to the Firebase website. From there, tap Studio in the top-right.
    • I would prefer local ai over online ai on some tasks . They are good enough for tasks like artificial voice , image editing , text corrections , tagging etc . Local AI on Windows Photo editor is impressive for example . There are probably many other Ai that we could run on simple pc with GPUs .
    • oh look, we are under the control of the U.S again, is it not about time we as a country did this ourselves if they want it, not rely on greedy U.S. tech companies? We are capable of doing so as a country. I hate how the U.K have gone with no industry like we used to have and relying on other countries. Maybe we should follow Trump words, and make Britain great again and stop bending over to please Trump and other countries. Our government is Trumps puppet. I know we need to co-operate on some things, but come on.
    • Actually I see no problem is notifying readers in advance. The post author mentioned that it'll be available on July 22.
    • We can actually disable this new AI powered tab groups suggestion feature. https://www.askvg.com/enable-o...mart-tab-groups-in-firefox/
  • Recent Achievements

    • Week One Done
      MIghty Haul earned a badge
      Week One Done
    • One Month Later
      MIghty Haul earned a badge
      One Month Later
    • Collaborator
      KD2004 earned a badge
      Collaborator
    • One Month Later
      ataho31016 earned a badge
      One Month Later
    • One Month Later
      Delahenty Machinery earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      593
    2. 2
      Michael Scrip
      201
    3. 3
      ATLien_0
      192
    4. 4
      +FloatingFatMan
      140
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!