Like Tree1Likes
  • 1 Post By Elysia

"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. #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
    ZuK
    ZuK is offline
    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
    ZuK
    ZuK is offline
    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
    Posts
    23,027
    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,342
    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
    Posts
    23,027
    Don't remove variable names in the declaration: SourceForge.net: Do not remove parameter names - cpwiki
    King Mir likes this.
    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, 12:47 PM
  2. stdin "incompatible pointer type" as FILE*
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 08-21-2008, 12:44 AM
  3. struct "dereferencing pointer to incomplete type"
    By modec in forum C Programming
    Replies: 8
    Last Post: 04-14-2005, 04: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21