Passing/Returning arrays to/from functions (N00b question)
Moderator: Coders of Rage
Re: Passing/Returning arrays to/from functions (N00b question)
ahh i see what you are saying. yeah that would work. but yeah. i still would never reccomend to use char[] as apposed to char*
you could still change that function to do all you would want. but yeah, i would still just use char*.
you could still change that function to do all you would want. but yeah, i would still just use char*.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- wtetzner
- Chaos Rift Regular
- Posts: 159
- Joined: Wed Feb 18, 2009 6:43 pm
- Current Project: waterbear, GBA game + editor
- Favorite Gaming Platforms: Game Boy Advance
- Programming Language of Choice: OCaml
- Location: TX
- Contact:
Re: Passing/Returning arrays to/from functions (N00b question)
Well, there really isn't a difference when you pass it into a function. void func(char items[]) is the same as void func(char* items). I was just saying that checking if the next item is NULL (which is defined to be 0L) would mean it would only work with null-terminated arrays. Which is fine, you just have to remember to make sure your array is null-terminated.avansc wrote:ahh i see what you are saying. yeah that would work. but yeah. i still would never reccomend to use char[] as apposed to char*
you could still change that function to do all you would want. but yeah, i would still just use char*.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: Passing/Returning arrays to/from functions (N00b question)
I R Lost
Anyone mind running this through in nub_cake terms?
Anyone mind running this through in nub_cake terms?
My love is like a Haddoken, it's downright fierce!
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Passing/Returning arrays to/from functions (N00b question)
Maevik, the cheap, not-necessarily-right way to do it is to make a struct or a class and do this:
This will make a copy of the array holder to pass and return. So modifying "holder" won't modify the original. Also, passing copies of arrays is generally bad, because it doesn't scale well.
However, if you pass a pointer, it points to the first element in the array, allowing you to do things like so:
Did that maybe help?
You can also pass it using the array, but you have to know the size in the parameter.
IE:
If I recall correctly. It works essentially the same operationally as the passAPointer.
I don't think that you can do
Where MY_ARRAY_CONSTANT is either a "magic number" or a "const int MY_ARRAY_CONSTANT = 1234567890;". Or more correctly, "const size_t MY_ARRAY_CONSTANT = 1234567890;"
Edit: Sorry, I didn't look at your code, but the text in your post seems like you understand your algorithm, just not how to pass arrays around.
Code: Select all
struct MyHolder {
int array[MY_ARRAY_CONSTANT];
};
MyHolder returnACopy(MyHolder holder) {
/* ... */
}
However, if you pass a pointer, it points to the first element in the array, allowing you to do things like so:
Code: Select all
char* takePointerReturnPointer(char* myString) {
/* You can work on myString as you would any other character array (c-style string)
But it will modify the original one, and return what can be treated as the original one */
cout << myString << endl;
cout << myString[0] << myString[1] << endl;
myString[2] = 'q';
return myString;
}
int main() {
char myCString[100] = "I'm a big winner!\0";
char* ptr;
ptr = takePointerReturnPointer(myCString);
cout << myCString << endl;
cout << ptr << endl;
return 0;
}
You can also pass it using the array, but you have to know the size in the parameter.
IE:
Code: Select all
void passAnArray(char someArray[MY_ARRAY_CONSTANT]) { }
I don't think that you can do
Code: Select all
char[] returnAnArray() { }
Edit: Sorry, I didn't look at your code, but the text in your post seems like you understand your algorithm, just not how to pass arrays around.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: Passing/Returning arrays to/from functions (N00b question)
This is helpful. I need to book up on string stuff I guess.
My love is like a Haddoken, it's downright fierce!
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Passing/Returning arrays to/from functions (N00b question)
C++ strings are easier than C-strings (character arrays), but the above works for arrays of any type.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: Passing/Returning arrays to/from functions (N00b question)
Code: Select all
char myCString[100] = "I'm a big winner!\0";
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Passing/Returning arrays to/from functions (N00b question)
As far as I understand it, yes.Scoody wrote:Isn't the \0 redundant there, since using "" adds one on the end automatically?Code: Select all
char myCString[100] = "I'm a big winner!\0";
But I'm not the one to ask about c-strings.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Passing/Returning arrays to/from functions (N00b question)
Me either, obviously. But the arrays-in-general example was fine. I hope.Ginto8 wrote:But I'm not the one to ask about c-strings. :( :lol:
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: Passing/Returning arrays to/from functions (N00b question)
im not entirely sure, but i dont know if i would call a character array a cstring
traditionally i know them as char *cString;
anyways. so i was searching about csting.... and oh my god!!!!! my next GF gets one of these.
https://www.cstringdirect.com/
traditionally i know them as char *cString;
anyways. so i was searching about csting.... and oh my god!!!!! my next GF gets one of these.
https://www.cstringdirect.com/
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Passing/Returning arrays to/from functions (N00b question)
"The CString can also be worn alone:
• Swimwear • Lingerie"
GOD YES!!!!!! YES!!!!!!!!
• Swimwear • Lingerie"
GOD YES!!!!!! YES!!!!!!!!
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- Kros
- Chaos Rift Regular
- Posts: 136
- Joined: Tue Feb 24, 2009 4:01 pm
- Current Project: N/A
- Favorite Gaming Platforms: PC, Playstation/2/3
- Programming Language of Choice: C++
- Location: Oregon,USA
- Contact:
Re: Passing/Returning arrays to/from functions (N00b question)
Holy shi-
YouTube ChannelIsaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.