Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 29th, 2004, 09:52 AM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Char array cin query (additional)

Thanks for all you replies to my recent "Basic char array and cin query". I was really wanting to understand what was going on rather than alternative solutions hence this new thread. I'm working my way through "Beginning C++" so I don't want to introduce things that I haven't yet covered in the chapters. This query arose from the solutions to one of the exercises. In the code below (extracted directly from solutions to exercises in the book), the function header for GetData has one param that is passed by reference and one that isn't yet it still seems to work and I can't understand why.
The array gets populated by cin and its value gets passed back to main() even though it's not passed by reference - why ? What's so special about cin ? What's so special about char arrays that you don't need to use
Code:
void GetData(int& number, char& name[])
It's only cin that seems to do this. I tested this out by adding a simple assignment statement in after the cin line (hence the commented out line: name = "John") and in a debugger, you can see that name gets set to John until the end of the function but then reverts to the value input using cin.
Code:
#include <iostream.h>
void GetData(int& number, char name[])
{
    cout << "Enter a number: \n";
    cin >> number;

    if (number != 0)
    {
        cout << "And a name: \n";
        cin >> name;
        //name = "John";
    }    
}
void PutData(int number,char name[])
{
    cout    << "Thank you.  Your number and name were " << number
            << " and " << name << "\n";
}
void main()
{
    int number;
    char name[15];
    for (;;)
    {
        GetData(number, name);
        if (number==0)
            break;
        PutData(number,name);
    }    
}
Gill BC
__________________
Gill BC
Reply With Quote
  #2 (permalink)  
Old October 29th, 2004, 11:24 AM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you use
Code:
strcpy(name, "John");
in the function, you will see that "John" persists.

When you have a function declared like
Code:
void GetData(int& number, char name[]);
It is exactly the same as if you had
Code:
void GetData(int& number, char *name);
And as a matter of fact when you call the function, the second argument is always treated as "char *name", even if you had declared it as "char name[]". That is and always has been a part of the C language (therefore C++).

In your function, consider the statement

[code]
name = "John";
[code]

This sets the local copy of "name" to point to the C constant array, but does not affect anything in the calling program.

The strcpy() that I suggested above actually copies "John" into the array, so that the main program prints out "John".



There is nothing special about char arrays, the same is true for other arrays. In the C language there is no "string" data type; standard library functions for manipulating strings (strcat, strlen, etc.) are based on having strings represented as null-terminated arrays of char.

Regards,

Dave
Reply With Quote
  #3 (permalink)  
Old October 29th, 2004, 03:03 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much Dave for such a clear explanation - exactly what I needed! I just hate to move on before I've understood exactly what's going on.

Gill BC
Reply With Quote
  #4 (permalink)  
Old October 29th, 2004, 03:22 PM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM
Invalid conversion from 'char*' to 'char' deuxmio C++ Programming 3 December 8th, 2006 07:56 AM
Basic char array and cin query. gillianbc C++ Programming 4 November 4th, 2004 07:44 PM
cin.getline alfrepa Visual C++ 0 April 14th, 2004 08:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.