How to get right arrays when I call the array function two times at the same line?

I have this function at class A:

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.
1
2
3
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 .




Last edited on
internal_char_array is a char array[5] declared as private for this class

Yet your public interface for class B provides a function that returns a pointer to this private internal array?
I have this function at class A:

void my_function(char array1[],char array2[]);
I call it so:

my_function(a,b, classb->give_me_array(22), classb->give_me_array(33));


But that function call doesn't match the function signature. One's got 4 parameters, the other 2.
It was a mistake. Now the original post is ok.
Hi tonnot
I'm not sure what the problem is you're looking to solve with this code but I think the design's going to cause you a lot of problems even if you fix this. Public interfaces shouldn't be providing pointers to private data :( That's why it's private.
Your function calls (to give_me_array) are both returning the same pointer, so the data's always going to be the same. The second function call overwrites the data that the first function call wrote there.
I'd suggest a design re-think. Could you post a bit more code to put this in context?
Last edited on
I have a 'byte' color rgb array (red green blue alpha and index itself)
I have a vector that stores, for each line-type, two colors ( by index.)
So I need to retrieve the rgb array.
So....
first I need a method to return the array. The only method is returning a pointer? ( I'd not want to pass first an array to the function... )
second. I pass this information to another function, and I'd like to write the line with two calls to the function that give me the array.
my_function(classb->give_me_array(22), classb->give_me_array(33));
Thanks
Your calling environment needs a place to store the data coming out of classb->give_me_array(). There's no way to make the same pointer point to two different sets of data at the same time. Maybe something like:

1
2
3
4
5
6
7
8
9
char arr1[5]; // allocate on stack
char arr2[5];

// Rewrite give_me_array so it takes a pointer-to-char (where data will be written) as well as
// your other int.  This is safer anyway because then you're not giving access to class B's internals.
classb->give_me_array( arr1, 22 );
classb->give_me_array( arr2, 33 );
// Now ready to call my_function().
my_function( arr1, arr2 );


I know that's not the solution you wanted but I don't think your way's possible ;) Unless one of the gurus here can chip in and offer one...
Thanks for the ideas
Topic archived. No new replies allowed.