Thread: Problem passing by reference

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    1

    Unhappy Problem passing by reference

    Hello

    This piece of code is supposed to print X but doesn't . It crashes. Any help in identifying the problem would be greatly appreciated !

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct W {
     char c;
     struct W *next;
    } list_node;
    
    
    list_node *insert_list (char c, list_node *next_node) {
     list_node *temp_node = malloc(sizeof(list_node));
     temp_node->c = c;
     temp_node->next = next_node;
     return temp_node;
    }
    
    
    void read_input (list_node *list) {
     char c = 'X';
     list = insert_list(c, list);
    }
    
    
    int main (void) {
     list_node *list = NULL;
     read_input(list);
     printf("%c", list->c);
     return 0;
    }
    However, if I change line 27 to

    Code:
    list = read_input(list);
    and change the read_input function to:

    Code:
    list_node *read_input (list_node *list) {
     char c = 'X';
     list = insert_list(c, list);
     return list;
    }
    then it does print X. But I do not want to return a value back to main(), I want it to pass by reference like the first example.

    Any suggestions?
    Last edited by Joe Bligh; 01-16-2012 at 10:10 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Joe Bligh
    But I do not want to return a value back to main(), I want it to pass by reference like the first example.

    Any suggestions?
    Since you want to pass the pointer by reference, pass a pointer to a pointer, e.g.,
    Code:
    list_node *list = NULL;
    read_input(&list);
    Of course, read_input has to change accordingly, e.g.,
    Code:
    void read_input(list_node **list) {
        char c = 'X';
        *list = insert_list(c, *list);
    }
    Note that you used malloc but neglected to free the memory allocated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing by value/reference?
    By Shmamy in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2011, 11:53 PM
  2. Problem Passing Structure Reference To Function
    By soj0mq3 in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 10:27 AM
  3. Passing by reference of by value?
    By abd in forum C Programming
    Replies: 32
    Last Post: 01-28-2010, 04:49 PM
  4. passing by reference
    By AngKar in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 09:31 PM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM