user3358702
user3358702

Reputation: 3

Error in call to function using pointers as parameter

Error message:

main.cpp: In function 'int main()':
main.cpp:39:52: error: expression list treated as compound expression in initializer [-fpermissive]
  int* concatArray (*pArray1, size1, *pArray2, size2);
                                                    ^
main.cpp:39:52: error: invalid conversion from 'int' to 'int*' [-fpermissive]

Line 39 of my code is where i call the concat function; i have never seen this error before and i do not know how to address it.

#include <iostream>
#include <cstdlib>

using namespace std;

int* concatArray (int* pArray1, int size1, int* pArray2, int size2);

int main (){

    int size1 = 8;
    int Array1 [size1];
    for (int i = 0; i < size1; i++){
        Array1[i] = rand() % 10;
    }

    int size2 = 10;
    int Array2 [size2];
    for (int i = 0; i < size2; i++){
        Array2[i] = rand() % 10;
    }

    int* pArray1;
    int* pArray2;
    pArray1 = Array1;
    pArray2 = Array2;

    cout << "The first array contains: " << endl;
    for (int i = 0; i < size1; i++){
        cout << Array1[i] << endl;
    }

    cout << "The second array contains: " << endl;
    for (int i = 0; i < size2; i++){
        cout << Array2[i] << endl;
    }

    int* concatArray (*pArray1, size1, *pArray2, size2);

    cout << "the concat array contains: " << endl;
    for (int i = 0; i < size1 + size2; i++) {
        cout << (concatArray + i) << endl;
    }
    return 0;
}

int* concatArray (int* pArray1, int size1, int* pArray2, int size2){

    int* concatArray = new int [size1 + size2];
    for (int i = 0; i < size1 + size2; i++) {
        if(i < size1){
            *(concatArray + i) = *(pArray1 + i);
        }
        else{
            *(concatArray + i) = *(pArray2 + i);
        }
    }
    return concatArray;
}   

Upvotes: 0

Views: 62

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

This expression

int* concatArray (*pArray1, size1, *pArray2, size2);

is invalid.

In fact it looks as a definition of a pointer to int with name concatArray and with some arguments.

Moreover if you meant a call of function concatArray and if you would write it correctly as

int *SomePointer = concatArray (*pArray1, size1, *pArray2, size2);

nevertheless its calli is also invalid because instead of pointers you pass as arguments objects of type int: *pArray1 and *pArray2.

Take into account that sizes of arrays shall be constant expressions in C++. So this code

int size1 = 8;
int Array1 [size1];
//...
int size2 = 10;
int Array2 [size2];

is not C++ compliant.

Also this loop in the function

for (int i = 0; i < size1 + size2; i++) {
   if(i < size1){
      *(concatArray + i) = *(pArray1 + i);
      }
   else{
      *(concatArray + i) = *(pArray2 + i);
      }
}

is invalid. Instead of

*(concatArray + i) = *(pArray2 + i);

there should be

*(concatArray + i) = *(pArray2 + i - size1);

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254461

int* concatArray (*pArray1, size1, *pArray2, size2);

That's muddling the syntax for declaring a variable and calling a function. To avoid confusion, you should choose a different name for the variable, and initialise it with the result of calling the function.

That should fix the first error; the second is because the first and third arguments should be pointers; but you're dereferencing the pointers to get the int values they point to.

So this should be something like

int* concatenated = concatArray(pArray1, size1, pArray2, size2);

Hopefully, you'll soon learn why it's a bad idea to juggle pointers like this, how to fix the memory leak in your code, and how to use library facilities to manage memory more easily. But that's rather beyond the scope of the question.

Upvotes: 3

Related Questions