Page 2 of 2

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

Posted: Tue Mar 03, 2009 3:21 pm
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*.

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

Posted: Tue Mar 03, 2009 3:28 pm
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.

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

Posted: Tue Mar 03, 2009 5:21 pm
by Maevik
I R Lost

Anyone mind running this through in nub_cake terms?

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

Posted: Tue Mar 03, 2009 6:56 pm
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.

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

Posted: Wed Mar 04, 2009 12:07 am
by Maevik
This is helpful. I need to book up on string stuff I guess.

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

Posted: Wed Mar 04, 2009 11:49 am
by MarauderIIC
C++ strings are easier than C-strings (character arrays), but the above works for arrays of any type.

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

Posted: Thu Mar 05, 2009 12:24 am
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?

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

Posted: Thu Mar 05, 2009 5:48 am
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:

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

Posted: Thu Mar 05, 2009 8:53 am
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.

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

Posted: Thu Mar 05, 2009 10:44 am
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

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

Posted: Thu Mar 05, 2009 10:45 am
by avansc
"The CString can also be worn alone:
• Swimwear • Lingerie"

GOD YES!!!!!! YES!!!!!!!!

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

Posted: Fri Mar 06, 2009 1:43 am
by Kros
Holy shi-