Passing/Returning arrays to/from functions (N00b question)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Passing/Returning arrays to/from functions (N00b question)

Post by avansc »

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*.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
wtetzner
Chaos Rift Regular
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)

Post by wtetzner »

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*.
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.
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.
User avatar
Maevik
Chaos Rift Junior
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)

Post by Maevik »

I R Lost

Anyone mind running this through in nub_cake terms?
My love is like a Haddoken, it's downright fierce!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Passing/Returning arrays to/from functions (N00b question)

Post by MarauderIIC »

Maevik, the cheap, not-necessarily-right way to do it is to make a struct or a class and do this:

Code: Select all

struct MyHolder {
int array[MY_ARRAY_CONSTANT];
};

MyHolder returnACopy(MyHolder holder) {
    /* ... */
}
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:

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;
}
Did that maybe help?

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]) { }
If I recall correctly. It works essentially the same operationally as the passAPointer.
I don't think that you can do

Code: Select all

char[] returnAnArray() { }
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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Maevik
Chaos Rift Junior
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)

Post by Maevik »

This is helpful. I need to book up on string stuff I guess.
My love is like a Haddoken, it's downright fierce!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Passing/Returning arrays to/from functions (N00b question)

Post by MarauderIIC »

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.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: Passing/Returning arrays to/from functions (N00b question)

Post by Scoody »

Code: Select all

char myCString[100] = "I'm a big winner!\0";
Isn't the \0 redundant there, since using "" adds one on the end automatically?
User avatar
Ginto8
ES Beta Backer
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)

Post by Ginto8 »

Scoody wrote:

Code: Select all

char myCString[100] = "I'm a big winner!\0";
Isn't the \0 redundant there, since using "" adds one on the end automatically?
As far as I understand it, yes. ;)

But I'm not the one to ask about c-strings. :( :lol:
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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Passing/Returning arrays to/from functions (N00b question)

Post by MarauderIIC »

Ginto8 wrote:But I'm not the one to ask about c-strings. :( :lol:
Me either, obviously. But the arrays-in-general example was fine. I hope.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Passing/Returning arrays to/from functions (N00b question)

Post by avansc »

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/

Image
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Passing/Returning arrays to/from functions (N00b question)

Post by avansc »

"The CString can also be worn alone:
• 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"
User avatar
Kros
Chaos Rift Regular
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)

Post by Kros »

Holy shi-
Isaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
YouTube Channel
Post Reply