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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:43:44+00:00 2026-05-26T00:43:44+00:00

I don’t know how to pass a pointer to functions and change the values

  • 0

I don’t know how to pass a pointer to functions and change the values that pointer pointing to,

I did it like this:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct node{
    struct node* lchild;
    struct node* rchild;
    struct node* p;
    int noChild;
    char* data;
    int height;
};

typedef struct node text_t;

text_t * create_text(){
    text_t * txt = NULL;
    return txt;
}

int length_text(text_t *txt){
    if(txt == NULL)return 0;
    return txt->noChild+1;
}

char * get_line(text_t * txt, int index){
     if(index>txt->noChild+1) return NULL;

     text_t * current = txt;
     while((current->noChild+1)!=index-1){
        if(index-1>current->noChild+1){
           current = current->rchild;
           }else{
             current = current->lchild;
             }                                                     
     }
     return current->data;
}

void append_line(text_t *txt, char * new_line){     
      text_t * temp;

      if (txt == NULL){

       txt = (text_t *)malloc(sizeof(text_t));
       txt->lchild = NULL;
       txt->rchild = NULL;
       txt->noChild = 0;
       txt->p = NULL;
       txt->data = new_line;
       txt->height = 1;
       printf(txt->data);
     }else{
       text_t * current = txt;

       while(current->rchild!=NULL){
       current = current->rchild;
       }

       temp = (text_t *)malloc(sizeof(text_t));
       temp->lchild = NULL;
       temp->rchild = NULL;
       temp->noChild = 0;
       temp->data = new_line;
       temp->height = 1;
       temp->p = current;

}}    







int main()
{  int i, tmp; text_t *txt1, *txt2; char *c;
   printf("starting \n");
   txt1 = create_text();//1
   txt2 = create_text();

   append_line(txt1, "line one" );//2
 ...

I want to create an object of such a struct in C and use it to build a balanced tree.

However, after I did append_line(), nothing changes with txt1, it’s still NULL.
Why?

Thanks

  • 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-26T00:43:45+00:00Added an answer on May 26, 2026 at 12:43 am

    It’s because c is pass by value.

     void append_line(text_t *txt, char * new_line){ 
       // ....
    
       txt = (text_t *)malloc(sizeof(text_t));
    
       // .....
     }
    
     int main() {
       // ...
       append_line(txt1, "line one" );
       // ...
     }
    

    txt1 in main isn’t affected with the malloc statement in the appen_line to txt. If you want to affect the txt1 in main then change the function text_t parameter to pointer to a pointer.

    void append_line(text_t **txt, char * new_line);
    

    Now pass the address of txt1 from main –

    append_line(&txt1, "line one" );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know if this has been asked before, but what i'd like to
Don't know if there is a better way to do this, so that is
I don't know how, bug I've arrived to a tree like this: X1--X2--(a lot
Don't know the difference between the System.Window.Controls.TextBox and System.Windows.Forms.TextBox . Noticed that the System.Windows.Forms.TextBox
Don't know what I did wrong. I have two indices with identical documents in
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
Don't know if this is the right place to ask this, but I will
Don't know what to do with this error. How to add data in SQL
Don't know if this is an eclipse specific problem but whenever I declare a

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.