Is the only way to return a string, as in a character array, to using a pointer array?
And if you cout the function that returns the character array, will it cout the contents or something else?
Just a generic example...
char Name(char a[])
{
return a[];
}
The function call...
cout << Name("Cristina");
This is a very generic example. The program I'm working on manipulates 3 char arrays and puts them in to one. I need to return the array. So my main question is do i need return a pointer array so that when i say cout << Name("Cristina"); Cristina is on the screen?
Paul McKenzie
February 19th, 2003, 10:45 PM
Why not use std::string? Your generic example won't work because you have a return type of char, which is a single character, not an array. You can't return an array anyway, so your example just won't work.
Anyway, there are C++ ways to solve a problem, and 'C' ways to solve a problem. Unless there is a good reason, you solve this using the C++ way, and that is using std::string. There is no need for messy pointers for what you want to do.
#include <string>
#include <iostream>
std::string Name(const std::string& sName)
{
return sName + " is 30 years old";
}
int main()
{
std::cout << Name("Christina");
}
Regards,
Paul McKenzie
SGSpecialK
February 19th, 2003, 11:40 PM
There are a lot of concepts that in I haven't learned yet in my curriculum. The problem that I am doing says without including <cstring> write a function that accepts three stings, first name middle name and last name and return one string with the for last, first, middle inital. My example is wrong and that was my question, how to make it correct and to make it work so that when i do return a char array and output the function in main it outputs the char array i returned. My question is how to do that. We haven't been exposed to std::string yet so I don't know what that is. Right now, I think my professors lesson is working with pointers and arrays together. Although for this problem it might be inefficent, it's what I was asked to do.
Paul McKenzie
February 20th, 2003, 05:25 AM
Originally posted by SGSpecialK
There are a lot of concepts that in I haven't learned yet in my curriculum. The problem that I am doing says without including <cstring><cstring> is not <string>. <cstring> contains the 'C' character string functions such as strcpy, strcmp, etc. <string> is the std::string class. I think you should ask your teacher to clarify exactly what they want. If you don't include <cstring>, you have to write your own function to concatenate the strings.
write a function that accepts three strings, first name middle name and last name and return one string with the for last, first, middle inital. My example is wrong and that was my question, how to make it correct and to make it work so that when i do return a char arrayYou can't return an array -- you must return a pointer to a new or existing array. That's the problem -- where does this new / existing array come from? Is it global, is it allocated with operator new[], is it a static char array within the function? The assignment you described needs more explanation.My question is how to do thatAgain, you or your teacher needs to explain what they want. There are many ways to do this, all different in design and approach.We haven't been exposed to std::string yet so I don't know what that isSigh. This sounds like another C++ course being taught like a 'C' course. Practically every C++ expert agrees that the first thing that you should have been exposed to is std::string, and not have char pointers introduced at the beginning stage. Your professor should read what Stroustrup (the inventor of the language) has to say about teaching C++ in this manner.
http://www.research.att.com/~bs/new_learning.pdf
Needless to say, Stroustrup and almost every C++ expert in the field disagrees with the approach your professor has taken.
Given that you can't change things, you are still justified in asking for more details.
make sure you use the strn functions (strncpy, strncat, _snprintf, etc ), so that you do not overflow your array. Is _snprintf() a standard function? If not, the function may not exist for the OP for the compiler they're using.
Regards,
Paul McKenzie
Andreas Masur
February 20th, 2003, 03:44 PM
Originally posted by Paul McKenzie
Is _snprintf() a standard function? If not, the function may not exist for the OP for the compiler they're using.
Regards,
Paul McKenzie
As you already have assumed correctly this is not a standard function...it is available only within the Windows C run-time library... :cool:
Bob Davis
February 20th, 2003, 06:10 PM
So I'm guessing the generally accepted way would be this:
// szFullName is a pointer that will point to a newly allocated string
char* MakeName( char* szFullName, size_t size, const char* szFirst, const char* szMiddle, const char* szLast )
{
if (size < strlen(szFirst)+strlen(szMiddle)+strlen(szLast)+1) return NULL; // make sure size is big enough
Originally posted by Bob Davis
So I'm guessing the generally accepted way would be this:
<snipped code>
No...since we are talking about C++ the generally accepted way would be the one Paul provided in the beginning... :cool:
Additionally in your example you should add that the caller needs to delete the returned string...
mwilliamson
February 21st, 2003, 09:26 AM
Originally posted by Bob Davis
So I'm guessing the generally accepted way would be this:
// szFullName is a pointer that will point to a newly allocated string
char* MakeName( char* szFullName, size_t size, const char* szFirst, const char* szMiddle, const char* szLast )
{
if (size < strlen(szFirst)+strlen(szMiddle)+strlen(szLast)+1) return NULL; // make sure size is big enough
No. You should not new the memory. The memory should be created before calling the function. Thats why the size value is passed, it not you could simply return the size. Also, your calculation is incorrect and you will overwrite the bounds of your array. You did not add 2 for your spaces. Which is why you should always use the strn functions.
Paul McKenzie
February 21st, 2003, 10:48 AM
Originally posted by mwilliamson
No. You should not new the memory.This is the way Bob Davis interpreted the original question by the OP. There are many ways to do what the OP described, Bob Davis did it one way, you did it another, and others will come up with other solutions.
Also, your calculation is incorrect and you will overwrite the bounds of your array. You did not add 2 for your spaces. Which is why you should always use the strn functions.
In general, this is why the use of std::string is done.
int main()
{
std::cout<< MakeName("Christina", "Violet", "Jones");
}
Even experienced programmers make mistakes such as not allocating for spaces, extra NULLs, etc. You would think that the goal of a C++ course is to create programs that are maintainable -- the assignment given by the teacher creates programs that are hard to maintain, causes bugs, etc. You wonder whether the teacher is actually a 'C' programmer not familiar with proper teaching of C++, or is just following a poorly laid out C++ curriculum.
Hopefully SGSpecialK has looked at the myriad of different 'C'-like answers in this thread and compared them with the simple answer of using std::string. Maybe he has learned that the proper way to alleviate all of these problems is to use the proper C++ techniques to handle character data -- and that is to use std::string.
Coincidentally, the answers in the thread illustrates exactly what Stroustrup was talking about in his paper concerning teaching C++ (see the link I provided).
Regards,
Paul McKenzie
stober
February 21st, 2003, 01:17 PM
The original poster wants to do it with char arrays, not STL classes, even though string would be the ideal way of doing it, he is obviously trying to learn char arrays, not string. So any solution using string is no solution at all.
Andreas Masur
February 21st, 2003, 03:37 PM
Originally posted by stober
The original poster wants to do it with char arrays, not STL classes, even though string would be the ideal way of doing it, he is obviously trying to learn char arrays, not string. So any solution using string is no solution at all.
Well...I do not see that he asked for a solution based on character arrays...
Is the only way to return a string, as in a character array, to using a pointer array?
Using 'string' is one...
SGSpecialK
February 21st, 2003, 06:50 PM
I'm a she not a he, just to clarify. Anyway here is the code I wrote.
int totalSize = sizeF + sizeL +5; return Name[totalSize];
}
I know it is wrong because you can't return a char array like Paul said.
I asked if I would have to use pointer arrays so that when i cout the function call in main like this
int main()
{
cout << FullName("George", "Walker", "Bush");
}
it would output Bush, George W. to the screen.
stober
February 21st, 2003, 07:05 PM
There are three problems with your function:
1. As you and others mentioned, the function must be declared to return char* instead of just char.
2. the last line, you return only one character of the Name array, you want to return the entire string.
3. Name is declared on the stack. When the function returns, the string occupied by the memory can contain any random stuff. You can (1) make Name a static object within the function, or (2) allocate it on the heap, or (3) pass it as a parameter from the calling function.
below I am not using your code as examples just to simplify it.