#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    New York
    Posts
    5
    Rep Power
    0

    Passing a string by value


    I am trying to pass the string firstName by value from the called function getFirstName to the calling function main. I am tring to set up an array of 20 characters for the user to enter their first name. I want the user to enter their name in the getFirstName function and then pass it through to the main function and display it from there with out using any global variables.
    Here is the program. Please help me if you can.

    #include <iostream.h>

    void getFirstName(char);


    void main()
    {
    char firstName[20];
    getFirstName(firstName);
    cout << "\nYou entered: " << firstName;

    }

    void getFirstName(char firstName[20])
    {
    cout << "\n\nPlease enter the first name: ";
    cin.getline(firstName,20);
    return;
    }
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Code:
    #include <iostream>
    using namespace std;
    
    void getFirstName(char firstName[]);
    
    
    int main()
    {
            char Name[20];
            getFirstName(Name);
            cout << "\nYou entered: " << Name << endl;
            return 0;
    }
    
    void getFirstName(char firstName[])
    {
            cout << "\n\nPlease enter the first name: ";
            cin.getline(firstName,20);
    }
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Ah, another Blaise'r who's become a C'er. I.e., you are transitioning from Pascal to C.

    In Pascal we do distinguish between "call by name" and "call by value", but not in C. In C there is not such thing as "call by name", every parameter is "called by value".

    However, in C we do accomplish the same thing as "call by name" when we pass a pointer. In Pascal we do it implicitly, whereas in C we do it explicitly.

    Therefore, it is impossible to pass a string "by value" -- in fact, there is only one way to pass a string. Because it is an array, you must always pass a pointer to the string -- remember that an array name is equivalent to a pointer for these purposes.

    Another note: when you declare a parameter to be an array, you do not specify its length. You only do that when you declare the array itself (see infamous41md's example).

    Also, this:
    void getFirstName(char *firstName)
    is equivalent to this:
    void getFirstName(char firstName[])


    BTW, there's a story about Niklaus Wirth, the designer of Pascal. During an interview, he was asked how to pronounce his name (he's Swiss). He responded that it depends. If you call him by name, then it's "Wirth" (as in the German), but if you call him by value, then it's "worth".
    (From "Real Programmers Don't Write Pascal")
  6. #4
  7. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    geek1099 may be using C++ (or may be using a compiler like MSVC which allows you to mix C and C++). If you are using C++ - and this is just for clarification - you do have the option to pass by value or pass by name (which is called pass by reference in C++), just as in Pascal.

    However, for the particular case of passing in an array - pass by reference does not help any, since arrays in C/C++ are actually just pointers. You can only pass in the pointer, not the entire array. So, whether you choose to pass the pointer by value or reference (in your case), it does not matter.

    However, in a function in which you are not passing arrays, you have both options available to you in C++, just as in pascal. Arrays are the only type that you cannot pass into a function in C/C++, because of the way C/C++ handles them as pointers.

    I hope I have not confused you...
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    New York
    Posts
    5
    Rep Power
    0
    I got the program working perfect thank you for your help.

IMN logo majestic logo threadwatch logo seochat tools logo