Character Array functions.

This is a discussion on Character Array functions. within the C++ Programming forums, part of the General Programming Boards category; Can I make a funtion that is a character array? What is the proper syntax?
I tried this but it ...

A comment on terminology:
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:

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.

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:

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?

RULE #1: Everything is passed by value.

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:

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?

I think the explanation for that goes like this, the original function:

Code:

char Foo ()
{
return "hello world";
}

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.

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!

As for this one:

Code:

char* charArrayFunc()
{
char dookey = 'K';
return &dookey;
}

ummm...erhhh...I guess char literals are treated the same as string literals.

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.

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.

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.