Like Tree1Likes
  • 1 Post By laserlight

Problem passing by reference

This is a discussion on Problem passing by reference within the C Programming forums, part of the General Programming Boards category; Hello This piece of code is supposed to print X but doesn't . It crashes. Any help in identifying the ...

  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 09:10 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    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.
    Joe Bligh likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10: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, 03: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, 12:25 AM

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