|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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);
}
|
|
#3
|
||||
|
||||
|
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") |
|
#4
|
||||
|
||||
|
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...
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." |
|
#5
|
|||
|
|||
|
I got the program working perfect thank you for your help.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Passing a string by value |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|