I am working with dynamic arrays which have been created using <vector>. For convenience, I have created a typedef :

typedef vector<double> C1DArray;

In the main program, several variables are created using this definition which, at the moment, means working with arrays of size 30.

. . .
// Declare several dynamic arrays that will be re-sized appropriately later.
C1DArray WORK1_Array, WORK2_Array, WORK3_Array, WORK4_Array;
. . .
return 0;

Several sub-routines only need to work with a particular range of these arrays. For example, SUB-ROUTINE1 takes in WORK2_Array and only touches, say, elements 5 through 25: WORK2_Array[4] - WORK2_Array[24]. In other words, it doesn't need to access all 30 elements of the array. I would like to pass the address of WORK2_Array[4] into the sub-routine so that, within the sub-routine, a simple for-loop could be used that starts the indexing at 0. However, I can't get it working. Following is one of the attempts I have made so far.

Define SUB-ROUTINE1 such that it takes in a pointer to the address of a specific element within a dynamic array (WA1):

void SUBROUTINE1 (int N, int L1, C1DArray CC, C1DArray& CH, C1DArray* WA1, C1DArray* WA2){
double variable1;
for (int i = 0, i < 20; i++) variable1 = WA1[i] + WA2[i];
CH[15] = variable1;

In the main program, I try to call SUB-ROUTINE1 as follows:

SUBROUTINE1(30, 5, Array1, Array2, &WORK2_Array[4], &WORK2_Array[17]);

However, the compiler does not like this code. I have tried other ways of doing it but am presently stumped. The error message says, "Cannot convert argument from "double" to "C1DArray."

How can I pass the address of a specific element of a dynamic array to a sub-routine so that, within the sub-routine, it can be treated as a regular array (though of reduced range)?

Your help is appreciated.

Recommended Answers

All 2 Replies

Have you ever worked with iterators? Using iterators you could do code like this.

typedef vector<double> C1DArray;
double sum(C1DArray::iterator it, C1DArray::iterator end)
    double sum = 0;
    while(it++ != end)
        sum += *it;
    return sum;

int main()
    C1DArray data;
    for(int i = 0; i < 30; i++)

    cout << sum(data.begin() + 4, data.begin() + 24);
    return 0;

Well, where to begin...

First things' first, you should not use all upper-case letters when naming your functions or variables. This is not Fortran77 code, this is C/C++ code. It is conventional in C/C++ code to reserve all upper-case letters for MACROs (or defines), i.e., things created with #define, to distinguish them from regular identifiers (names of functions, classes and variables). Whichever style you choose, I don't care, but don't use all upper-case style (it's not the 70s anymore, and we don't use punched cards anymore!).

The real solution to your problem is stated directly in that statement you made yourself: ".. pointer to the address of a specific element within a dynamic array ..". If WA1 is supposed to be a pointer to a specific element, and your elements are of type double, then why is it that you declared WA1 as C1DArray* WA1. Here's what you need:

void subroutine1(int n, int l1, const C1DArray& cc, C1DArray& ch, double* wa1, double* wa2) {
    double variable1 = 0.0;
    for (int i = 0; i < 20; i++) 
        variable1 = wa1[i] + wa2[i];
    ch[15] = variable1;

But in general, you should be using iterator ranges instead, as is typical of generic C++ algorithms (e.g., transform).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.