Thread: "Subscript requires array or pointer type."

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    "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];

    What's wrong? Thanks in advance!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    double innerProduct(double * vectorA, double * vectorB, int dim)
    should do the trick

    Kurt

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    2
    Why did that fix it? I haven't learned what that means but what happens when you add asterisks?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by nrr5094 View Post
    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. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    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. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't remove variable names in the declaration: SourceForge.net: Do not remove parameter names - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-08-2009, 11:47 AM
  2. stdin "incompatible pointer type" as FILE*
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 08-20-2008, 11:44 PM
  3. struct "dereferencing pointer to incomplete type"
    By modec in forum C Programming
    Replies: 8
    Last Post: 04-14-2005, 03:39 PM
  4. "subscript requires array or pointer type"?
    By Nutka in forum C Programming
    Replies: 12
    Last Post: 12-06-2002, 05:51 PM
  5. Replies: 4
    Last Post: 11-09-2002, 01:16 PM

Tags for this Thread