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 7433939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:45:11+00:00 2026-05-29T09:45:11+00:00

I would like to get some help with the following question. I have a

  • 0

I would like to get some help with the following question.
I have a struct Node and I want to change it’s insides using some method.
and I want to print the content of the changed struct inside my main method.
how do I get the struct changed and see the changed value in my main without returning the node as a return value.
I guess it might be solved with passing the struct Node as a pointer and then editing it.
what is the right way to do so?

for example:

typedef struct Node{
   struct Node * right;
   struct Node * left;
   void * data;
}Node;

void insert(void * element, Node* root){
   if(root==NULL){
       root=(Node*)malloc(sizeof(Node));
       root->data=element;
   }
}


int main(){
    Node a;
    int b=8;
    insert(&b,&a);
    printf("%d",*(int*)a.data);
    return 0;   
}

printf doesn’t print 8 it prints 1 (i guess some garbage)

  • 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-29T09:45:12+00:00Added an answer on May 29, 2026 at 9:45 am

    It sounds like you are trying to do the following

    • Create a struct in one method, say main
    • Pass it to a second method, say example
    • Have example modify the struct and have the results visible in main

    If so then the way to do this in C is by passing the struct as a pointer to example.

    struct Node {
      int data;
      struct Node* pNext;
    };
    
    void example(struct Node* pNode) {
      pNode->data = 42;
      pNode->pNext = NULL;
    }
    
    int main() {
      struct Node n;
      example(&n);
      printf("%d\n", n.data);
    }
    

    EDIT

    Responding to the updated question.

    To see the result of a modification of a Node you must pass a Node*. And accordingly to see the result of a Node* modification you need to pass a Node**. Essentially you need to pass one more level of indirection than the value you want to mutate / return.

    void insert(void* element, Node** ppRoot){
      if (NULL == *ppRoot) {
        Node* pTemp = malloc(sizeof(Node));
        pTemp->data = element;
        *ppRoot = pTemp;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get some nested params. I have an Order that has
If possible I would like to get some ideas to get the following done
I have a number of links which I would like to get some information
Some help with some T-SQL would be most appreciated. I have the following four
I would like to get some information on list of points that needs to
I would like to get some feedback on what is one of my first
I would like to get some clarification regarding lazy loading and session boundaries etc.
What I would like to get some input on is how to remove certain
I'm a lone developer who would like to get some structure to my projects.
I am running C# framework 2.0 and I would like to get some of

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.