Thread: Passing variables to functions

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

    Passing variables to functions

    Hi,

    I'm trying to pass a variable to testVars(), have testVars() change the variable, and then make that variable available to main().

    Passing the variable from main() to testVars() is working--getting it back to main() is not.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char testVars(char *a, char *b);
    
    main()
    {
        char * testChar = "We just passed";
        char * testCharTwo = "a main() variable!";
        testVars(testChar, testCharTwo);
    
        printf("%s %s - Back to main()!\n", testChar, testCharTwo);
    
        return 0;
    }
    
    char testVars(char *a, char *b)
    {
        b = "a variable!";
        printf("%s %s\n", a, b);
    }
    The expected result is that the program will print the following two lines:

    We just passed a variable!
    We just passed a variable! - Back to main()!

    But what I'm getting is:

    We just passed a variable!
    We just passed a main() variable! - Back to main()!

    I'm new to C and am still in the early stages of learning it, but it's my understanding that if I pass a pointer variable from main(), in my case, to testVars() that testVars() is actually working with the dereferenced location of testCharTwo (since this is the only one I wish to change in testVars()) and any modifications it makes to testCharTwo should be immediately available to main().

    Am I missing something here? Or is my understanding of this not correct?

    ~ Tom

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Within your function testVars, the variable b is local to the function and changes to it disappear once the function ends. This is the same as when passing anything to a function, if you need to have it changed by that function when it ends, you need to pass a pointer. In this case what you are passing in is already a pointer to begin with but to have that changed by the function what you need to pass is still the address of that variable (a pointer to a pointer).
    Code:
    void test(const char** testp);
    
    int main()
    {
        const char * mytestptr = "Hello World!\n";
    
        printf("%s",mytestptr);
        test(&mytestptr);  // Pass address of mytestptr, a pointer to a pointer
        printf("%s",mytestptr);
    
        return 0;
    }
    
    void test(const char** testp)
    {
        *testp = "Goodbye World\n";
    }
    Last edited by hk_mp5kpdw; 02-06-2012 at 11:21 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    You are working in C.
    b is a char *.
    In the function you assign to this "a variable!" this means that the address of this string (that is somewhere in the memory) is used in testVars.
    When the function returns, b points again the initial address.
    You can try with a strcpy(b,"a variable!"); this copies the content in the actual address b. But be sure that the new string is shorter than the first!

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    You need to use the dereference symbol at b = "a variable"

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    Quote Originally Posted by hk_mp5kpdw View Post
    Within your function testVars, the variable b is local to the function and changes to it disappear once the function ends. This is the same as when passing anything to a function, if you need to have it changed by that function when it ends, you need to pass a pointer. In this case what you are passing in is already a pointer to begin with but to have that changed by the function what you need to pass is still the address of that variable (a pointer to a pointer).
    Thanks, this worked!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void testVars(char *a, char** b);
    
    int main()
    {
        char * testChar = "We just passed";
        char * testCharTwo = "a main() variable!";
        testVars(testChar, &testCharTwo);
    
        printf("%s %s - Back to main()!\n", testChar, testCharTwo);
    
        return 0;
    }
    
    void testVars(char *a, char** b)
    {
        *b = "a variable!";
        printf("%s %s\n", a, *b);
    }
    Last edited by theitsmith; 02-06-2012 at 12:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing the variables into array by using functions C
    By zidanelam in forum C Programming
    Replies: 11
    Last Post: 07-07-2011, 10:58 AM
  2. Problem with passing variables between functions
    By Martin_T in forum C Programming
    Replies: 9
    Last Post: 11-23-2009, 03:26 AM
  3. passing variables between functions
    By owi_just in forum C Programming
    Replies: 6
    Last Post: 05-08-2005, 09:12 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. passing variables to functions?
    By aoe in forum C Programming
    Replies: 12
    Last Post: 06-02-2002, 04:19 PM