Hey I'm making a program that gives me a few strings from an array depending on how many I ask for. The arrays are in a separate class in a .h file. But I want to be able to transfer those strings from one function of the class to another, and the only way I could think of doing that is by returning the array back to the .cpp file and then back to a different function in the .h file. Could I just do something like "return exarray[];"? Or even could I use return in a for loop?
Did your teacher show you where things live in memory ?
You cannot return a local array as it is on the stack and will be gone after you exit the function. You will need to put the memory in the heap or pass the array in.
e.g.
char *func()
{
char testarry[]={'0','1','2','3'}'
return testarry; // illegal as this lives on the stack
}
you need to understand which door variables live in - heap,stack,data segment.
Nevermind, I get that you can't pass an array. Originally what I was doing was creating the array in the .cpp file and then passing it to the .h file where it was altered. But I kept getting an error that I had no clue what it meant, so I just decided to try it a different way. Apparently I accidentally had a semicolon somewhere that it shouldn't be and I completely missed it every time I looked through :P. Sorry about that.