#### Archived

This topic is now archived and is closed to further replies.

# Is it possible?

This topic is 6518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been thinking of this for a while - never really bothered to look into it, and have always worked around it. So, rather than fight with it for an indeterminant amount of time, I decided to ask around here. The problem: You have two arrays, one of a determined length, and one of an indetermined length, but longer than the first. In the first array (of determined length) all values are to point to values within the second array (of indetermined length). A few examples to clarify the problem further:
  //Here's a class that holds the first array: class test { public: int (&array1)[3]; poly(int (*newvals)[3]) : array1(*newvals) {} }; //And here's the second array: int array2[somevaluebiggerthan3]; 
So, could you assign 3 values from array2 to array1 apon initialization of the test class? Thanks, -Medgur Edited by - Medgur on 7/16/00 2:08:45 PM

I guess not.

##### Share on other sites
Is this what you mean?

class test{public: test(int array1[3]) { for( int x=0; x < 3; x++ ) this->array1[x] = array1[x]; } int array1[3];};int array2[15];void main(){ test object(array2);}

You can''t assign the array in the initializer list of the test class''s constructor. Instead, you have to use a for loop or just manually type in the three copies.

Happy Coding!

- null_pointer
Sabre Multimedia

##### Share on other sites
The problem is that it''s not an array, it''s a reference to an array of 3 values, and therefore must be initialized in the class''s constructor.

Thanks,
-Medgur

##### Share on other sites
You can do it using pointers:

class test{public: test(int array1[3]) : array1(array1) {} int* array1;};int array2[15];void main(){ test object(array2);}

Using ''int& array[3]'' creates an array of references, which cannot be initialized properly and is therefore illegal.

Using ''int array[]'' is a non-standard extension to use arrays of an unspecified size in a class.

Using pointers, you can specify the size of the array, and it works just like an array. However, that will not prevent you from using more than 3 elements in your code or passing in NULL or invalid pointers.

- null_pointer
Sabre Multimedia

##### Share on other sites
Hmm... Also, it doesn''t allow you to specify certain elements of the array (which 3) to pass. I haven''t tried the route you mentioned yet, guess I''ll go toss that around now.

Thanks,
-Medgur

##### Share on other sites
Since you know you want 3 elements, and elements in an array are stored contiguously in memory in the right order, all you need to do is pass the first element.

For example, to pass elements 13, 14, 15 from the global array2 to the test object, you would replace the main() function with this:

void main(){test object(&array2[12]);}

Arrays are zero-based, so that''s why I used a 12. Unfortunately, I don''t know of a way to keep you from passing an invalid element.

Happy Coding!

- null_pointer
Sabre Multimedia

• 39
• 15
• 9
• 23
• 10