The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Passing a string by value
Discuss Passing a string by value in the C Programming forum on Dev Shed. Passing a string by value C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 16th, 2003, 12:16 PM
|
|
Junior Member
|
|
Join Date: Apr 2003
Location: New York
Posts: 5
Time spent in forums: < 1 sec
Reputation 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;
}
|

April 16th, 2003, 12:30 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
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);
}
|

April 16th, 2003, 02:29 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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")
|

April 16th, 2003, 06:56 PM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
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...
|

April 22nd, 2003, 10:48 AM
|
|
Junior Member
|
|
Join Date: Apr 2003
Location: New York
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
I got the program working perfect thank you for your help.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|