Can I make a funtion that is a character array? What is the proper syntax?
I tried this but it didn't work.
Code:char Foo[50] (); //Int Main was here with all sorts of stuff. char Foo () { RETURN "HELLO WORLD"; }
Can I make a funtion that is a character array? What is the proper syntax?
I tried this but it didn't work.
Code:char Foo[50] (); //Int Main was here with all sorts of stuff. char Foo () { RETURN "HELLO WORLD"; }
I would say you can't do this because it is a function not a data type and is not accessed like a data type.
you'd have to use character pointers:just be sure to look into pointers so you don't introduce memory leaks into your program.Code:#include<iostream> char*foo(); int main() { char*out=foo(); std::cout<<out; std::cin.get(); return 0; } char*foo() { return "Hello World"; }
Last edited by major_small; 03-29-2005 at 11:55 PM. Reason: warning: memory leak possible
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
A comment on terminology:Can I make a funtion that is a character array?
A function performs some task. A function can accept data and peform some operation on that data, and a function can return data. But a function is not a character array, it is a function. It can accept a character array as data, or it can return a character array, but the function itself cannot be a character array.
It seems like a function should be able to return a character array because you can do this:
The function is able to accept a character array as a parameter.Code:#include<iostream> using namespace std; void display(char str[]) { cout<<str<<endl; } int main() { char my_str[] = "hello world"; display(my_str); return 0; }
But, you cannot do this:
The function is unable to return a character array.Code:#include<iostream> using namespace std; char[] display(void) { return "hello world"; } int main() { cout<<display()<<endl; return 0; }
However, what you need to realize is: an array name is really a pointer, and for whatever reason, the return type will not let you use array notation as a substitute for pointer notation.
Last edited by 7stud; 03-30-2005 at 02:07 PM.
That is wrong because if you returned "HELLO WORLD" you would be returning the address of that string. Plus even if you changed the function header to say char* Foo(), you would still be returning the address to a local variable (I think this is correct) which is a big no-no.Originally Posted by SlyMaelstrom
Just to make sure I got this right, returning a string literal would be the same as returning an address to a local char array, right? The address wouldn't contain anything useful if used in the calling function, right?
EDIT: Shoot, I tested the returning of the address of the string literal and then displaying the string and it worked. Could someone please explain why this happens? However, it doesn't work when I try to return the address of a regular array. Even setting it up like this worked:
Why is this?Code:#include <iostream> using namespace std; char* charArrayFunc() { char* dookey = "Hello World!"; return dookey; } int main() { char* fooFoo = charArrayFunc(); cout<<fooFoo<<endl; cin.get(); return 0; }
Alright this is ridiculous! I even did this and it still works! What is up with it?! This is going against everything I've ever learned about local variables!
Code:#include <iostream> using namespace std; char* charArrayFunc() { char dookey = 'K'; return &dookey; } int main() { char* fooFoo = charArrayFunc(); cout<<*fooFoo<<endl; cin.get(); return 0; }
Last edited by homeyg; 03-30-2005 at 04:30 PM.
RULE #1: Everything is passed by value.Shoot, I tested the returning of the address of the string literal and then displaying the string and it worked. Could someone please explain why this happens?
What that means is that a copy is always made when you send an argument to a function AND when you return a value from a function. So, when you return a string literal, a copy of that address is sent back to main(), and a copy of an address is just as good as the original address. Then, the << operator is programmed to ouput a string instead of the address for the type "pointer to char", which is why you can do this:
char str[] = "hello world";
cout<<str<<endl;
or this:
char* p = "goodbye";
cout<<p<<endl;
Last edited by 7stud; 03-30-2005 at 06:35 PM.
I think the explanation for that goes like this, the original function:Just to make sure I got this right, returning a string literal would be the same as returning an address to a local char array, right?
never declared a named variable, so nothing goes on the stack(i.e. the thinga-ma-jig that keeps track of temporary local variables)--similar to when you use the new operator. Instead, string literals get stored elsewhere in memory, so they are not targets for destruction when the funtion ends. Another characteristic of a string literal is that if another variable in the program is assigned the same string literal, it gets the same address. In that regard, the string literal is a constant, so you can't change it. If you could change the string literal, that would mess up the other variables that were assigned the address of that same string literal.Code:char Foo () { return "hello world"; }
Applying those musings to this function:
The string literal is placed in that special area of memory I surmised about earlier, and then its address is assigned to dookey. In the return statement, a copy of the address in dookey is returned to main(). Then, dookey is destroyed when the function terminates, but the unnamed string literal "Hello World!" is safely hidden away somewhere's!Code:char* charArrayFunc() { char* dookey = "Hello World!"; return dookey; }
As for this one:
ummm...erhhh...I guess char literals are treated the same as string literals.Code:char* charArrayFunc() { char dookey = 'K'; return &dookey; }
Last edited by 7stud; 03-30-2005 at 07:29 PM.
Yeah but the function with the char literals, I also tried it with a local type int variable, and cout in main was still able to display it.
It still worked!Code:int* charArrayFunc() { int dookey = 3444; return &dookey; } int main() { int* whatever = charArrayFunc(); cout<<*whatever<<endl; return 0; }
Now, what I'm thinking (after I flipped through a few of my C++ books) is that the variable dookey goes out of scope, its former address hasn't been reused yet, so it retains the value it originally contained. That's what I concluded after an hour of reading. The same goes for char variables because I don't think char literals represent their address and aren't treated the same way as string literals.
Last edited by homeyg; 03-30-2005 at 10:31 PM.
Homeyg, what compiler are you using? Returning a pointer (or reference) to a local normally generates a warning. As to why that "works" it's one of the charming things about C/C++, there are very few errors when it comes right down to it, what there is a lot of is UNDEFINED BEHAVIOR This can range from a Blue Screen of Death to, far more commonly, doing what you expected. In this case the address is still "there" because addresses are just numbers. Even after your fuction returns whatever was written in memory is probably still there. It's just that it will likely be used for something else the next time you call a function. Even pointers to things that you have deleteed might still have whatever was in there before, and the operating system may still consider that address to be within your program. Then at some point later, when the operating system is looking for more space it may decide that the same address is part of a different program. This is when the ever-popular General Protection Fault/ Segmentation Fault/ Bus Error happens.
I use Dev-C++. Thanks for the explaination. That's what I figured the deal was all along.Originally Posted by grib
Last edited by homeyg; 03-31-2005 at 03:33 PM.