How to get an 'instant copy' of an array returned by a function used two times ....
The title space is not enough to explain my problem.
I have a my_function at class A:
Code:
void my_function(char array1[],char array2[]);
I call it so:
my_function(classb->give_me_array(22), classb->give_me_array(33));
'Give_me_array' located at another class B, receive an index and fill the internal array with 5 values.
Code:
char * give_me_array(int index) {
fill internal_array....
return internal_char_array;}
internal_char_array is a char array[5] declared as private for this class
Ok, my problem is that my_function receives the value for 33 also for 22. ( I think that this is a pointer problem) . How to solve this ?
I'd not want to use two aditional arrays and function calls to solve the problem.
Any idea ?
I'd need a trick to make an instant copy ....
Thanks
Re: How to get an 'instant copy' of an array returned by a function used two times ..
Code:
void give_me_array(int index, char* outputArray) {
fill internal_array....
strcpy(outputArray, internal_char_array);
}
When you call give_me_array, make sure "outputArray" has enough space (i.e. greater than 5 bytes if your internal_char_array is always char array[5)
Re: How to get an 'instant copy' of an array returned by a function used two times ..
Ok, but to do this I need an external outputArray ....
As I know I can call this function two times in the same line, a trick could be having two internal arrays to return the internal_array1 or internalarray2 ... ?
What do you think ?
Re: How to get an 'instant copy' of an array returned by a function used two times ..
Well, what can I tell you, your code design is wrong.
Why you want to return a pointer that points to your private class member ? Any user of your class may change your private member without your knowledge.
In the best case I would return a const pointer, but this is also a bad scenario in some situations.
Re: How to get an 'instant copy' of an array returned by a function used two times ..
Why don't you just use QList or QVector ? It will save you a lot of coding.
Re: How to get an 'instant copy' of an array returned by a function used two times ..
Thanks to everybody and for the ideas