For this: such that array196 and array20 would be pointing to the same location you can do: int *arr2 = arr1 + 96; assert(arr20 == arr196 == 97).
For this: "such that array196 and array20 would be pointing to the same location. " you can do: int *arr2 = arr1 + 96; assert(arr20 == arr196 == 97).
That assert should be a mathematician asserting, not C :-) – Alok Jan 26 '10 at 3:42 this works! Thank you! – John Jan 26 '10 at 3:45 +1 for the simple solution that works.
– Chris Lutz Jan 26 '10 at 4:28.
A reference hack from a C programmer willing to subvert the type system to get what works: int (&array2)5 = (int (&)5)(*(array1 + 5)); Now array2 will be an array for all intents and purposes, and will be a sub-array of array1, and will even be passable to that famous C++ array_size template function. Though the best way to handle this hackery is to hide it with more hackery! #define make_sub_array(type, arr, off, len) (type (&)len)(*(arr + off)); int (&array2)5 = make_sub_array(int, array1, 5, 5); Nice.
Terrible by some standards, but the end result a) looks pretty neat, b) does exactly what you want, c) is functionally identical to an actual array, and d) will also have the added bonus (or mis-feature) of being an identical reference to the original, so the two change together. UPDATE: If you prefer, a templated version (sort of): template T (&_make_sub_array(T (&orig)M, size_t o)) { return (T (&))(*(orig + o)); } #define make_sub_array(type, array, n, o) (type (&)n)_make_sub_array(array, o) int (&array2)5 = make_sub_array(int, array1, 5, 5); We still have to pass the type. Since one of our arguments must be used as part the cast, we cannot cleanly (IMHO) avoid the macro.
We could do this: template T (&make_sub_array(T (&orig)M, size_t o))N { return (T (&)N)(*(orig + o)); } int (&array2)5 = make_sub_array(array1, 5); But the goal here is to make the calling code as clean as possible, and that call is a bit hairy. The pure-macro version probably has the least overhead and is probably the cleanest to implement in this case.
It's so bad it's good :-). +1. – Alok Jan 26 '10 at 4:08 Thanks.It took me much longer because I wanted to make it work with actual C++ casting, but eventually I gave up and made it work.
Improvements (especially a potential version of make_sub_array as a template function - I tried that for a while as well before giving up) welcome. – Chris Lutz Jan 26 '10 at 4:09 @Chris: My C++-fu isn't great, I am more of a C person, but I will try! – Alok Jan 26 '10 at 4:13 That's slick... er ugly... I mean, I'm impressed.
The macro helps readability, but the declaration of array2 is still sort of weird. +1 for the fu – John Knoeller Jan 26 '10 at 4:18 I could make the macro #define make_sub_array(type, orig, new, off, len) type (&new)len = (type (&)len)(*(orig + off)) so that it's called as a single statement: make_sub_array(int, array1, array2, 5, 5); but I would (personally) prefer to see the assignment in my code so that it's clear that we're declaring and defining a variable. – Chris Lutz Jan 26 '10 at 4:27.
For a completely different approach you could do something like. Vector v0(array1 + 95, array1 + 100); or vector v1(array1, array1 + 100); vector v2(v1.begin() + 95, v1.end()); This would make a real copy of the elements of your vector.
Should be std::vector for pendantry. Anyway, +1 for the most "C++" answer. – Chris Lutz Jan 26 '10 at 4:50.
You can use boost::iterator_range to represent "slices" of arrays/containers: #include #include int main() { int array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // Create a "proxy" of array5..7 // The range implements the concept of a random sequence containter boost::iterator_range subarray(&array5, &array7+1); // Output: 3 elements: 5 6 7 std::cout The usefulness of Boost. Range will become more apparent once you learn about STL containers and iterators. If you're into linear algebra, Boost.
UBlas supports ranges and slices for its matrices and vectors.
In C++ you can use an int pointer as an int array, so getting the array2 to start at item 96 in array1 is easy, but there isn't any way to give array2 a size limit, so you can do this int array2 = &array196; or this int *array2 = &array196; but NOT this int array25 = &array196; // this doesn't work. On the other hand, C++ doesn't enforce array size limits anyway, so the only real loss is that you can't use sizeof to get the number of elements in array2. Note: &array196 is the same thing as array+96 edit: correction - int array = &array96 isn't valid, you can only use as a synonym for * when declaring a function parameter list.So this is allowed extern int foo(int array2); foo (&array196).
I still get an error when I try to type in : int array2 = &array196. The error that I receive is : cannot convert from 'int *' to 'int ' – John Jan 26 '10 at 3:42 @John: int array2 = &array196; is bad. – Alok Jan 26 '10 at 3:46 Yeah, int foo and int *foo are interchangeable only as function parameters.
– jamesdlin Jan 26 '10 at 3:57 @james: They aren't really interchangeable. Rather, arrays can decay into pointers, but they aren't the same. – GMan Jan 26 '10 at 4:10 @GMan: They are interchangeable as function parameters (not arguments).
That's unrelated to the decay thing. – jamesdlin Jan 26 '10 at 4:59.
Int array1 = {1,2,3,...99,100}; int *array2 = &array196.
Int arr = { 1, 2, 3, 4, 5}; int arr12; copy(arr + 3, arr + 5, arr1); for(int I = 0; I.
You said you don't want to copy the array, but get a pointer to the last five elements. You almost had it: int array1 = {1,2,3,...99,100}; int* array2 = &array195.
When I tried that I got this error: cannot convert from 'int *' to 'int ' – John Jan 26 '10 at 3:36 1 int array2 = array1+95; is illegal. – Alok Jan 26 '10 at 3:42 Should be int array1 = {1,2,3,...99,100}; int* array2 = array1+95; You cannot initialize an array (square brackets) with a pointer -- it can tell where it starts but not its size. – BenG Jan 26 '10 at 3:46.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.