# "Subscript requires array or pointer type."

This is a discussion on "Subscript requires array or pointer type." within the C++ Programming forums, part of the General Programming Boards category; Hey, all, Very new to C++ programming. I'm attempting to code a function that takes the inner product of two ...

1. ## "Subscript requires array or pointer type."

Hey, all,

Very new to C++ programming. I'm attempting to code a function that takes the inner product of two vectors and it's giving me the error message: subscript requires array or pointer type. Here is my code:

Code:
```#include <iostream>#include <string>
using namespace std;

double innerProduct(double vectorA, double vectorB, int dim);

int main(){
int dim;
int i;

double vectorA[100];
double vectorB[100];

cout << "What is the dimension of the two vectors: (Enter an integer)\n";
cin >> dim;

while(dim <= 0 || dim > 100){
if(dim <= 0){
cout << "Enter an integer greater than 0.\n";
cin >> dim;
}
else{
cout << "Enter an integer less than or equal to 100.\n";
cin >> dim;
}

cout << "Enter " << dim << " numbers for vector A.\n";

for(i=0; i<dim; i=i+1)
cin >> vectorA[i];

cout << "Enter " << dim << " numbers for vector B.\n";

for(i=0; i<dim; i=i+1)
cin >> vectorB[i];

cout << "The inner product of Vectors A and B of dimension " << dim;
cout << " is " << innerProduct(vectorA, vectorB, dim) << "\n";
}

return 0;
}

double innerProduct(double vectorA, double vectorB, int dim){
double ip;
int i;

double cp[100];

ip=0;

for(i=0; i<dim; i=i+1)
cp[i]=vectorA[i]*vectorB[i];

for(i=0; i<dim; i=i+1)
ip=ip+cp[i];

return ip;
}```
The error message in terminal appears twice and refers to line 53:
cp[i]=vectorA[i]*vectorB[i];

2. Code:
`double innerProduct(double * vectorA, double * vectorB, int dim)`
should do the trick

Kurt

3. Why did that fix it? I haven't learned what that means but what happens when you add asterisks?

4. Originally Posted by nrr5094
I haven't learned what that means
So how come you could write the body of innerProduct() ?
Anyway, arrays become pointers to the first element if you pass them to functions and pointers behave similar to arrays in c/c++

have you learnt it this way ?
Code:
`double innerProduct(double vectorA[100], double vectorB[100], int dim);`
should work as well
Kurt

5. I suggest you get to learn std::array or std::vector lest you make buffer overruns or corrupt data. Also declare variables near first use. You can declare them in the initialization part of the for loop too!
Some material to help you: SourceForge.net: Safer arrays in Cpp - cpwiki

6. The left most dimension of an array, matrix, ... , does not need to be specified in a function, so you could use:

Code:
```double innerProduct(double vectorA[], double vectorB[], int dim)
{
/* ... */
}```
and for the declaration:

Code:
`double innerProduct(double [], double [], int);`

7. Don't remove variable names in the declaration: SourceForge.net: Do not remove parameter names - cpwiki