Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6656129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:36:19+00:00 2026-05-26T01:36:19+00:00

I am new to c++, and am trying to port a program i have

  • 0

I am new to c++, and am trying to port a program i have made in python to c++. I have a struct that is a linked list with a list of parts. Each of these parts contains one or more components. So i have tried to create two structs, where one struct links to the other struct.

But i dont seem to get the list_part to link to component_list.

struct list_part {
    char partname[100];
    int parttype;
    component_list * comp;
    list_part * next;
};

struct component_list {
    char compname[100];
    component_list * next;
};

I use the following function to add the part to the bottom of the list.

void addpart(char partname[], int parttype, component_list *newcomp) {
    struct list_part *temppart;
    struct list_part *currentpart;

    temppart = (struct list_part *)malloc(sizeof(struct list_part));
    strcpy_s(temppart->partname,partname);
    temppart->parttype = parttype;
    temppart->comp = newcomp;

    currentpart = head;

    if (head == NULL) {
        head = temppart;
        head->next = NULL;
    } else {
        while (currentpart->next != NULL) {
            currentpart = currentpart->next;
        }
        temppart->next = NULL;
        currentpart->next = temppart;
    }
}

And a similar function to add the component to a list.

void addcomp(char compname[]) { 
    struct component_list *tempcomp;
    struct component_list *currentcomp;

    tempcomp = (struct component_list *)malloc(sizeof(struct list_part));
    strcpy_s(tempcomp->compname,compname);

    currentcomp = newcomp;

    if (currentcomp == NULL) {
        currentcomp = tempcomp;
        currentcomp->next = NULL;
    } else {
        while (currentcomp->next != NULL) {
            currentcomp = currentcomp->next;
        }

        tempcomp->next = NULL;
        currentcomp->next = tempcomp;
    }
}

When the first component in a part is present i try to add it with.

struct component_list *newcomp = NULL;
strcpy_s(compname,temp.c_str());
addcomp(compname);

and the rest of the components i was planing to add with these commands

strcpy_s(compname,temp.c_str());
addcomp(compname);

And finally this is added as a part with

addpart(partname,fluidname, parttype, newcomp);

When i do it this way the newcomp only returns 00000000, but i need it to return a pointer to the list with components for this part. I have no idea how to do this really, i am used to dynamic languages, and this is not an issue there. I have figured this is the best way to go about this, but am very open to suggestions for other solutions. As data structures is something i am very fresh at.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:36:20+00:00Added an answer on May 26, 2026 at 1:36 am

    You have 2 big mistakes in addcomp function, this should work: (also moved some things)

    void addcomp(char compname[]) { 
        struct component_list *tempcomp;
        struct component_list *currentcomp;
    
        tempcomp = (struct component_list *)malloc(sizeof(struct component_list/*FIX 1*/));
        strcpy_s(tempcomp->compname,compname);
        tempcomp->next = NULL; /*Better do it here*/
    
        if (newcomp == NULL) {
            newcomp = tempcomp;/*FIX 2*/
        } else {
            currentcomp = newcomp; /*Better do it here*/
            while (currentcomp->next != NULL) {
                currentcomp = currentcomp->next;
            }
            currentcomp->next = tempcomp;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to port a python program to c#. Here is the line
I'm trying to port a C console program over to iPhone. I have imported
I have a program I am trying to port to linux. The program is
I'm new to Android development, i'm trying to port an IOS app to Android.
Ok, this may have been answered, but I'm new and trying to learn, so
I have 2 threads running a chat program such that one is a server
I'm trying to create a client-server program that send and receive a string (its
I am trying to port a C++ program from Linux to Windows using cygwin.
I trying to port a WinForms project to WPF. But I am have some
I'm new at pogramming and I have a problem that I cant solve. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.