Receiving error: subscript is not of integral type
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Receiving error: subscript is not of integral type

  1. #1
    Join Date
    Jul 2004
    Location
    Fredericksburg, Va
    Posts
    9

    Red face Receiving error: subscript is not of integral type

    I'm re-writing a program that used template-functions without the template functions. I receive an error: "subscript is not of integral type" on the second printarray function but the other two work fine. All it should do is pass the decimal numbers to the array but for some reason it is not storing the numbers and returning all of them. If I remove the second function the other two run fine. Can anyone helpwith this ????

    Thanks very much.

    Novice programmer seeking help :-(

    code follows....

    #include <iostream>

    using std::cout;
    using std::endl;

    void printarray_a (int a[], int aCount) {
    for (int x = 0; x < aCount; x++)
    cout << a[x] << " ";
    cout << "\n";
    }

    void printarray_b (double b[], int bCount) {
    for (double y = 0; y < bCount; y++)
    cout << b[y] << " ";
    cout << "\n";
    }

    void printarray_c (char c[], int cCount) {
    for (char z = 0; z < cCount; z++)
    cout << c[z] << " ";
    cout << "\n";
    }

    int main ()
    {
    int a[] = {1, 2, 3, 4, 5};
    double b[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
    char c[] = "HELLO";

    cout << "Array a contains:" << endl;
    printarray_a (a,5);

    cout << "Array b contains:" << endl;
    printarray_b (b,7);

    cout << "Array c contains:" << endl;
    printarray_c (c,6);

    return 0;
    }

  2. #2
    Join Date
    Jul 2002
    Location
    Boston, MA
    Posts
    335
    I would say that you problem is this:
    Code:
    double y
    subscript can not be double (try to get this to work array[1.5] ).

    BTW the type of the couter you use in your loop has nothing to do with the data type of your array.

  3. #3
    Join Date
    Jul 2004
    Location
    Fredericksburg, Va
    Posts
    9

    Question Can subscript be int?

    please explain your response.

  4. #4
    Join Date
    Jul 2002
    Location
    Boston, MA
    Posts
    335
    Ok, let me try. Let's take a look at your function as you wrote it:
    Code:
    void printarray_b (double b[], int bCount)
    {
        for (double y = 0; y < bCount; y++)
            cout << b[y] << " ";
        cout << "\n";
    }
    Now let's break it down.
    First of all your function definition:
    Code:
    void printarray_b (double b[], int bCount)
    Because you defining the first parameter as double b[] the entire array will be duplicated in the memory when the function is called. More appropriate way to do it would be to just pass a const pointer to the array:
    Code:
    void printarray_b (const double *pb, int bCount)
    Secondly, - your loop:
    Code:
    for (double y = 0; y < bCount; y++)
    Your goal is to iterate through all array elements and print each one of them. Right? The number of elements in array is passed into the function as a parameter and stored in variable bCount of int datatype. To be safe you always want to define your loop counter to be of the same type as the end count value (int in your case). This is why I said that the datatype of your counter has nothing to do with the datatype of your array elements.

    At last, - the line that causes your compilation error:
    Code:
    cout << b[y] << " ";
    Here you are trying to access y element in array b. Your problem is that y is defined as double and array subscript is not allowed to be defined as double. This definitely makes sence because you can not have array element with fractional index/position identifier (elements with indexes 1.5, 2.25, 3.17, etc. make no sence instead every element has integer index such as 0, 1, 2, 3, etc.).

    P.S. Just re-read the whole thing. This may be a confusing explanation (feel rusty after Friday night ). williamsr, let me know if this makes sence to you. Good luck.

  5. #5
    Join Date
    Jul 2004
    Location
    Fredericksburg, Va
    Posts
    9

    Thumbs up You've been a big help

    I really appreciate the way you broke down the code your explanation of the pointer in the function, subscripting with an integer and defining the loop counter the same as the end count. I really understand arrays and functions better than I did when I posted.

    Thanks again.

    Enjoy the rest of your weekend

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center