C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 16th, 2003, 12:16 PM
geek1099 geek1099 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: New York
Posts: 5 geek1099 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to geek1099
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;
}

Reply With Quote
  #2  
Old April 16th, 2003, 12:30 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
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);
}

Reply With Quote
  #3  
Old April 16th, 2003, 02:29 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,141 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 52 m 21 sec
Reputation Power: 1974
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")

Reply With Quote
  #4  
Old April 16th, 2003, 06:56 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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...

Reply With Quote
  #5  
Old April 22nd, 2003, 10:48 AM
geek1099 geek1099 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: New York
Posts: 5 geek1099 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to geek1099
I got the program working perfect thank you for your help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Passing a string by value

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap