Thread: Call-by-reference

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    12

    Call-by-reference

    If I have a simple program like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void change (char *t);
    
    int main (void)
    {
    char *test = "hellooo\n";
    printf("%s", test);
    change(test);
    printf("%s", test);
    return 0;
    }
    
    void change (char *t)
    {
    t = "Hi!\n";
    }
    How do I make it so that test would become "Hi!"? What am I doing wrong? THanks.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Either return the pointer, or make the function parameter a pointer to a pointer.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void change (char **t);
    
    int main (void)
    {
    char *test = "hellooo\n";
    printf("%s", test);
    change(&test);
    printf("%s", test);
    return 0;
    }
    
    void change (char **t)
    {
    *t = "Hi!\n";
    }

  3. #3
    Registered User wavering's Avatar
    Join Date
    Dec 2001
    Posts
    26
    Sorenson,

    That was useful! By returning the pointer do you mean something like:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *change (){
      return "Hi!\n";
    }
    
    int main (void){
      char *test = "hellooo\n";
      printf("%s", test);
      printf("%s", change(test));
      return 0;
    }
    The above works but I have had difficult experinces with strings and welcome your comments! Incidentally, being lazy and wishing to avoid errors I always put my main last - is there any benefit in doing an initial declaration of the function - it seems needlessly repetitive?

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >By returning the pointer do you mean something like ..snip

    Yep.


    >is there any benefit in doing an initial declaration of the function - it seems needlessly repetitive?

    I don't think so, but if you've a lot of functions it'll be easier to check their parameters, and return type if you've a list of prototypes at the top, and they could be in different files.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    char *test = "hellooo\n";
    //I think if you are going to change "test", if needs to be declared:
    char test[30] = "hellooo\n";
    //Although what you have will probably work.

    void change (char *t)
    {
    strcpy(t,"Hi!\n");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. call by reference
    By Jackie in forum C Programming
    Replies: 3
    Last Post: 10-29-2001, 06:46 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM