Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

Having trouble returning array pointers from a function. Not sure how exactly to do it properly. eg. char * returnArray(char string[]) { return string; } I'm unsure if I have to make a pointer variable. I get warnings if I try to return &string.

OpenStudy (e.mccormick):

Are you calling it with the string? If so, that is a call by reference and no return is needed.

OpenStudy (anonymous):

I'm doing something like this: char * returnCharPtr(char string[]) { char otherString[20] = "bonjour monsieur"; char *c_ptr; strcpy(otherString,string); c_ptr = &otherString; printf("returning %s", otherString); return c_ptr; } This is my attempt so far at making a a function return a pointer. I'm unsure if the pointer variable is necessary here. I need it to return the pointer to the array so that the array will be able to be assigned as another array. eg. int main(){ char somestring[20] = returnArray(string); }

OpenStudy (e.mccormick):

You can just make the array, then pass by reference to fill it. Here is an old project of mine: http://pastebin.com/6aEFrGtS Note the declaration of: `void psudoStats(long double arr[], int x);` and then how the array is used in it. Even though it is a void, I am using this to fill the array. ``` void psudoStats(long double arr[], int x) { int sd3, sd2, sd1, i, j; srand (time(NULL)); sd3 = x / 5; sd2 = x / 27; sd1 = x - sd2 - sd3; for (i = 0; i < x; i++) { for (j = 0; j < sd3; j++) { arr[i] = static_cast<long double>(9500.0 + (rand() % 500)); if (i % 2) arr[i] *= -1.0; } for (j = 0; j < sd2; j++) { arr[i] = static_cast<long double>(6800.0 + (rand() % 2700)); if (i % 2) arr[i] *= -1.0; } for (j = 0; j < sd1; j++) { arr[i] = static_cast<long double>((rand() % 6800)); if (i % 2) arr[i] *= -1.0; } arr[i] /= 100.0; } }// end psudoStats ```

OpenStudy (e.mccormick):

See, you don't pass an array. Never have in c/c++. You pass a pointer to the array. That has a whole world of implications. "At some moment we may need to pass an array to a function as a parameter. In C++ it is not possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address. In practice this has almost the same effect and it is a much faster and more efficient operation." From: http://www.cplusplus.com/doc/tutorial/arrays/

OpenStudy (rsmith6559):

Its_Sonic, there's also one other problem with your example: otherString is declared in the function. When the function returns, it's execution frame is reclaimed/destroyed, so a pointer into that area points to garbage.

OpenStudy (e.mccormick):

Ah, good point. I forgot to mention garbage collection!

OpenStudy (anonymous):

@Its_Sonic Do you want any more help?

OpenStudy (anonymous):

Is this C or C++?

OpenStudy (e.mccormick):

Not sure. IS has not replied in a while....

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!