-
Char array getter
Hey all,
I am writing a program using cstrings only and I was wondering if this would be the correct way to make a getter for a char array.
Code:
typedef char Name[MAX_LENGTH + 1];
Name last;
// Get the last name of the passenger
Name GetLastName()
{
return last;
}
Thanks in advanced!
-
No, you can't return an array, even if you typedef it.
Since you posted on C++, the best thing to do is use std::string.
But in C, the only useful way is to pass a pointer to a char array, where you want the answer to be stored, along with a length parameter.
fgets() is a really good model for this style of interface.
-
Unfortunately, no. You can't return arrays (of chars or of anything) directly as a return value. You can return a pointer, which works as long as you can guarantee that the object will remain in scope. For example, this will work
Code:
typedef char Name[MAX_LENGTH + 1];
Name names[10];
char *getName(int i) {
return names[i];
}
But this will not, because the array will have gone out of scope after the function returns.
Code:
char *getName(int i) {
Name name;
return name;
}
It is syntactically valid but you'd probably get segmentation faults when running it. Your array-returning syntax will be a syntax error, though. Have you tried it?
-
So is there a way to do the GetLastName function with cstrings or is that just impossible?
-
I have not tried it because it is causing other problems later in my program that is why I am wondering if it would work.
-
Yes, like I said. You can return a pointer -- which is like an array in many respects, but you need to ensure the lifetime/existence of the variable you are returning. There are many ways to do that, including just creating the data as an ordinary array outside the function as a global/static/class member variable, or simply passing it into the function; or you can use dynamic memory allocation. But if you're going to all that effort, std::string would really be easier. By the way, you can convert an std::string to a C-string at any time with the .c_str() function. There's very little need to use C-strings directly in C++.
-
Well the truth is it is a programming assignment teaching us how to use cstrings and cctypes. That is the reason I cannot use strings :p
-
Well perhaps you can tell us more about what the assignment requires, and why you think you need a "char array getter".
[edit] Post #8000! [/edit]
-
Well it is maintenance on an older program that we used and I need different things like enumeration new data types and other different things. The reason I think I need this char array getter is because the last time I wrote this program I made this last name getter so last could be received by other classes. The reason for this is because last is private.
-
Perhaps you should see where the getter is being called to see if the functionality belongs in the class that knows the name. In any case, if the Name is declared within a class then a member function of that class can safely return a pointer to the char array, since the array will be in scope as long as the class is in scope.
Code:
typedef char Name[BUFSIZ];
class X {
private:
Name name;
public:
char *getName() const { return name; }
};
If you have an array, just stating the name of the array is enough to get "&array[0]", i.e. a pointer to the first element.
-
But if I do char getName() it will not be able to fit in all the characters of the name. Correct? Or I am missing something?
-
There's a difference between returning a single char as you've suggested, and returning a char* (which is a pointer to chars). A char* pointer just points somewhere in memory and says "there are chars here, go wild". It doesn't tell you how many chars there are, you're supposed to know through other means. The standard for C-strings is to have a NULL termination character, so that you can tell where the string ends.
Most standard functions that deal with strings don't actually take char arrays, they take pointers. (This is the only way to accept as an argument a string of any length.) For example, strlen is declared as
Code:
size_t strlen(const char *s);
which basically means it takes a char* or char pointer as an argument and returns a size_t (an integer type) as a return value.
Similarly, you can return a char* which points to anywhere within an array of characters (including the beginning), and a calling function can take the char* and start walking through memory treating the data there as a string.
Try it. You might find that it works. :)
-
Well I was re-reading the assignment and it has to be void GetLastName( Name lastName ) would that change using the size_t strlen(const char *s); you suggested?
-
It looks like that function is passing in an array, expecting the last name to be copied into the variable, e.g.
Code:
Name name;
GetLastName(name);
So you have one char array inside the class, and one char array passed in as a parameter, and you want to put the data from one into the other. In a word, strcpy. :)
-
Oh so I will be putting the char array into this getter then I will just have that array copy to another array are you saying?