Thread: convert Linked list in C++ to C

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    12

    Exclamation convert Linked list in C++ to C

    Hellow everyone,

    I have a problem in converting a C++ code to C.

    Code:
    struct node
    {
    int id;
    string name;
    int idP;
    string nameP;
    double price;
    int unit;
    node *nextC; //idk how to convert to c
    };
    
    
    node *temp; //idk how to convert to c
    node *headC = NULL; //idk how to convert to c
    then, there are a part that using this.. which is :
    Code:
    temp = new node;
    temp-> id= tempid;
    temp->name= tempname;
    temp-> idP= tempidP;
    temp->nameP= tempnameP;
    temp-> price= tempprice;
    temp->unit= tempunit;
    temp-> nextC = headC;
    headC= temp;
    please help :( I totally no idea how to convert them.. TT

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    struct node
    {
    int id;
    string name;
    int idP;
    string nameP;
    double price;
    int unit;
    struct node *nextC;
    };
    
    
    struct node *temp; 
    struct node *headC = NULL;
    Look up the "malloc" function to replace "new"; remember "free" function will likely be needed.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    12
    Quote Originally Posted by stahta01 View Post
    Code:
    struct node
    {
    int id;
    string name;
    int idP;
    string nameP;
    double price;
    int unit;
    struct node *nextC;
    };
    
    
    struct node *temp; 
    struct node *headC = NULL;
    Look up the "malloc" function to replace "new"; remember "free" function will likely be needed.

    Tim S.
    Hellow, thanks for your fast respond, really appreciate it.

    I tried :
    Code:
    temp = malloc(sizeof(node));
    temp-> id= tempid;
    temp->name= tempname;
    temp-> idP= tempidP;
    temp->nameP= tempnameP;
    temp-> price= tempprice;
    temp->unit= tempunit;
    temp-> nextC = headC;
    headC= temp;
    
    and it throw an error
    Code:
    Undeclared identifier 'node'.
    ><

    EDIT : Nvm, I found that I should use temp = malloc(sizeof(struct node)); instead
    Last edited by Milo Juak MuTou; 12-06-2012 at 02:48 PM.

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Milo Juak MuTou View Post
    Nvm, I found that I should use temp = malloc(sizeof(struct node)); instead
    It is usually easier to use the pattern
    Code:
    temp = malloc(sizeof *temp);
    as you don't actually need to remember the type of temp this way. sizeof is an operator that only looks at the size of the operand, here the data type pointed to by temp.

    Just remember to read that line of code as "dynamically allocate one temp".

    If you wanted to allocate memory for an array of say five elements, then
    Code:
    temp = malloc(5 * sizeof *temp);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  2. Replies: 1
    Last Post: 03-16-2010, 10:17 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM