Like Tree3Likes
  • 3 Post By Nominal Animal

convert Linked list in C++ to C

This is a discussion on convert Linked list in C++ to C within the C Programming forums, part of the General Programming Boards category; Hellow everyone, I have a problem in converting a C++ code to C. Code: struct node { int id; string ...

  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
    2,614
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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 01:48 PM.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    847
    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, 05:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 01:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21