1

To access the array indice at the xth position we can use some sort of illustration as shown below

#include<iostream>
using namespace std;
int main(){
    float i[20];
    for(int j=0;j<=20;j++)
        i[j]=0;
}

However the following piece of code does not work

#include<iostream>

using namespace std;

float oldrand[55];
int jrand;

void advance_random(){
    int j1;
        float new_random;

    for(j1=0;j1<=23;j1++){
        int temp = j1+30;
        new_random = (oldrand[j1]) - (oldrand[temp]);
        if(new_random <0.0)
            new_random = new_random+1;
        oldrand[j1] = new_random;
    }
    for(j1=24;j1<=54;j1++){
        new_random[j1] = oldrand[j1] - oldrand[j1-23];
        if(new_random[j1]<0.0)
            new_random[j1] = new_random + 1;
        oldrand[j1]=new_random;
    }
}

I recieve the following error

ga.cpp:20: error: invalid types ‘float[int]’ for array subscript
ga.cpp:21: error: invalid types ‘float[int]’ for array subscript
ga.cpp:22: error: invalid types ‘float[int]’ for array subscript

I am not able to find a mistake in my code please help me

8 Answers 8

7

new_random isn't declared as an array of floats, it's declared as a float. The compiler is trying to tell you you can't index into a float.

Sign up to request clarification or add additional context in comments.

Comments

5

As others have noted, new_random is not declared as an array, hence the compiler error.

Moreover, with this type of loop

float i[20];
for(int j=0;j<=20;j++)
    i[j]=0;

you are going to run out of the array bounds and get undefined behaviour. Proper form is

for(int j=0;j<20;j++)

This is because in C/C++, arrays are indexed from 0, thus an array of 20 elements contains elements indexed from 0 to 19.

Comments

0

oldRand is an array new_random is just a float so

new_random[j1]

is invalid.

Comments

0

You declared new_random as a float, not an array of floats, and are trying to use the array method of accessing it (new_random[j1]).

Comments

0

This part of your first snippet is wrong:

int main(){
    float i[20];
    for(int j=0;j<=20;j++)
        i[j]=0;
}

The last iteration of that loop will assign to i[20] which does not exist (because float i[20] defines an array of floats from i[0] to i[19]). The right way is:

int main(){
    float i[20];
    for(int j=0;j<20;j++)
        i[j]=0;
}

Note that j<20 and not j<=20

3 Comments

Technically it does exist, it just isn't what he thinks it is.
@Blindy: No, technically it doesn't exist. There are no semantics that define what that pseudo-element would be.
@Tomalek, unless you're right at the edge of the 32-bit (or 64-bit) address range, i+20 will always exist.
0

Because you are treating the variable new_random as an array when you have declared it as a normal float.

1 Comment

Wow.. Seems the "this question has a new answer" banner didn't work this time :)
0

new_random is not an array. You are trying to treat it as one in the following lines:

new_random[j1] = oldrand[j1] - oldrand[j1-23];
        if(new_random[j1]<0.0)
            new_random[j1] = new_random + 1;

Comments

0

These are the lines causing the problem:

for(j1=24;j1<=54;j1++){ 
    new_random[j1] = oldrand[j1] - oldrand[j1-23]; 
    if(new_random[j1]<0.0) 
        new_random[j1] = new_random + 1; 
    oldrand[j1]=new_random; 
} 

As has been said, the "new_random[j1]" is illegal - it should work correctly as soon as you remove the "[j1]":

for(j1=24;j1<=54;j1++){ 
    new_random = oldrand[j1] - oldrand[j1-23]; 
    if(new_random<0.0) 
        new_random = new_random + 1; 
    oldrand[j1]=new_random; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.