char reference in function
Hi guys,
I'm new to C++ and I'm wondering if it is possible to referrence a char in a function?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#inlcude <iostream>
using namespace std;
void welcome(char& name[12])
{
cout << "Please can you enter your name: ";
cin >> name;
}
int main()
{
char name[12];
welcome(name);
cout << "Hello " << name;
system("pause");
return 0;
}
|
I know this can be done with a string or int if your using numbers.
If there is no way around this, would you be able to explain why.
thanks,
tecmeister.
Line 5 says the parameter is a 12 element array of char references. That is not what you're looking for.
Because arrays are simply pointers, you could do this:
1 2 3 4 5
|
void welcome(char* name)
{
cout << "Please can you enter your name: ";
cin >> name;
}
|
Thank you for that.
Another question:
Would you use a char or string?
Thanks again,
tecmeister.
The only time I use a char array to hold a string is when existing code requires it. I try very hard to use string instead of char*.
Topic archived. No new replies allowed.