1

I have a function to which I need to pass an array.

But I don't want to pass the entire array (e.g., valid indices from array[0] to array[size-1]), but a subarray (e.g., valid indices starting at array[5] to array[size-1]).

Is there a way to do that in C++?

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
voximdo
  • 53
  • 2
  • 14
  • 1. Classic C++ array hasn't conception of 'subarray'. 2. Cost remain the same, array is passed by pointer, You can pass array and two indexes. – Jacek Cz Sep 12 '16 at 07:39
  • 2
    Or you can use iterator concept by passing pointer to the first element and pointer after the last element. – KIIV Sep 12 '16 at 07:56
  • 3
    You cannot pass an array to a function. When you try to, you actually pass the address of the first element of the array. If you need a subarray that starts at 5, you just pass the address of the fifth elements. You shouldn't be using C-style arrays anyway. Use std::vector and iterators, this is the C++ way. – n. m. could be an AI Sep 12 '16 at 08:55

2 Answers2

0

You can transfer array to function parameter below

void Foo(int* arr, int length);
//call Foo
Foo(&a[0], length); //or
Foo(a, length);

you can also transfer a certain range of array.

Foo(&a[1], length);
Foo(a + 1, length);

Just, simple code.

#include <iostream>
void Print(int* arr, int length)
{
    for(int i=0; i < length; i++)
    {
        std::cout << *(arr + i) << ", ";
    }

    std::cout << std::endl;
}

int main()
{
    int a[5] = {1,2,3,4,5};

    //type A
    Print(&a[0], sizeof(a)/sizeof(int)); //print all element of a
    Print(&a[1], 3); //print 2,3,4

    //type B
    Print(a, sizeof(a)/sizeof(int)); //print all element of a
    Print(a + 1, 3); //print 2,3,4

    getchar();
    return 0;
}
hyun
  • 2,135
  • 2
  • 18
  • 20
0

Quoted comment by n.m.:

You cannot pass an array to a function. When you try to, you actually pass the address of the first element of the array. If you need a subarray that starts at 5, you just pass the address of the fifth elements. You shouldn't be using C-style arrays anyway. Use std::vector and iterators, this is the C++ way.

As indicated, you can add an offset to the array base pointer (and subtract from the passed arraysize accordingly).

Or pass begin and end (one past the end) pointers of the array to the function to achieve an "iterator-style" implementation.

But as you are programming C++, please consider to use std::vector.

Example:

#include <iostream>

void foo(int arr[], int size) {
    for (int i = 0; i < size; i++)
        std::cout << arr[i] << ' ';
}

void bar(int* begin, int* end) {
    while (begin != end)
        std::cout << *begin++ << ' ';
}

int main() {
    int arr[] = {0,1,2,3,4,5,6,7,8,9};
    int size = sizeof(arr)/sizeof(*arr);

    // pass entire array
    foo(arr, size);
    //bar(arr, arr+size);
    std::cout << '\n';

    // pass array starting at index 5
    foo(arr+5, size-5);
    //bar(arr+5, arr+size);
    std::cout << '\n';
}

The output is:

$ g++ test.cc && ./a.out
0 1 2 3 4 5 6 7 8 9 
5 6 7 8 9 
Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187