Thread: invalid types 'int[int]' for array subscript

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    25

    invalid types 'int[int]' for array subscript

    Hi,

    I'm trying to code Dijkstra by using STL, but I keep getting this error. I must be missing but I can't find it, here is the code:

    Code:
    #include <iostream>
    #include <cstdio>
    #include <set>
    #include <cstdlib>
    #include <ctime>
    #include <fstream>
    #include <vector>
    #include <algorithm>
    #include <utility>
    
    #define ii pair<int,int>
    #define MAXV 1000
    #define UNDIRECTED 1
    #define MAX 0x7fffffff
    
    int startv = 1, finalv = 0, nvertex, nedge;
    using namespace std;
    FILE *in,*out;
    //ifstream in("
    
    void dijkstra();
    void read_file();
    
    vector<ii > m[MAXV];
    
    int main() {
        read_file();
        dijkstra();
    }
    
    void read_file() {
        in = fopen("girdi.in","r");
        fscanf(in,"%d%d",&nvertex,&nedge);
        finalv = nvertex;
        for (int i=0; i<nedge; ++i) {
            int v1,v2,w;
            fscanf(in,"%d%d%d",&v1,&v2,&w);
            m[v1].push_back(ii(v2,w));
            if (UNDIRECTED) m[v2].push_back(ii(v1,w));
        }
    }
    
    void dijkstra() {
        set<ii > Q;
        int d[MAXV]; for (int i=0; i<MAXV; i++) d[i] = MAX;
        d[startv] = 0;
        Q.insert(ii(startv,d[startv]));
        while (!Q.empty()) {
            ii cur = *Q.begin();
            Q.erase(Q.begin());
            int v = cur.first, d = cur.second;
            for (vector<ii >::iterator it = m[v].begin(); it !=  m[v].end(); it++) {
                int v2 = it->first, cost = it->second;
                if (d[v2] > d[v] + cost) {
                    if (d[v2] != MAX) Q.erase(Q.find(ii(v2,d[v2])));
                    d[v2] = d[v] + cost;
                    Q.insert(ii(v2,d[v2]));
                }
    
            }
        }
        printf("%d\n",d[finalv]);
    }
    and here are the errors:
    Code:
    main.cpp: In function `void dijkstra()':
    main.cpp:54: error: invalid types `int[int]' for array subscript
    main.cpp:54: error: invalid types `int[int]' for array subscript
    main.cpp:55: error: invalid types `int[int]' for array subscript
    main.cpp:55: error: invalid types `int[int]' for array subscript
    main.cpp:56: error: invalid types `int[int]' for array subscript
    main.cpp:56: error: invalid types `int[int]' for array subscript
    main.cpp:57: error: invalid types `int[int]' for array subscript
    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You have declared an int variable d in the while loop that shadows the int array d in dijkstra().
    Kurt

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    Thanks!

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Error in Array

    I think I have the same problem in my program, which is mainly about matrix multiplication on a call function bases. the error is
    :230: error: invalid types `float[int]' for array subscript
    for the main equation, I wonder if you could help me with that
    many thanks

    #include <string.h>
    #include <fstream>
    #include <math.h>
    #define r 4
    #define c 4
    #define X 5
    #define Y 5
    #define P 4
    int x,y,p;
    int i,j,k;

    float Vi[X][Y][P]={};
    float Vr[X][Y][P]={};
    float S[r][c]={(1,1,1,-1),(1,1,-1,1),(1,-1,1,1),(-1,1,1,1)};
    void M_Matrix(float *Vi,float *Vr,float *S );
    void swap_connect(float *a , float *b);

    int main ()

    {
    int K;
    int MK=8;
    float I;
    float L=0.1;
    float Ztl=1;
    float Z=1;
    float H;
    float Ex;
    float Ref,Ref2,Ref3,Ref4;
    float Ey;

    for(K=0;K<MK;K++)

    {

    for(x=0;x<X;x++)

    {

    for(y=0;y<Y;y++)

    {

    M_Matrix(&Vi[x][y][0],&Vr[x][y][0],&S[r][c]);


    I=(2*(Vi[x][y][1]))+(2*(Vi[x][y][4]))-(2*(Vi[x][y][3]))-(2*(Vi[x][y][2]))/(4*Ztl);

    H=I/4*Ztl;

    Ex=-(Vi[x][y][1]+Vi[x][y][3])/L;

    Ey=-(Vi[x][y][2]+Vi[x][y][4])/L;

    }
    }


    }

    getch();

    return(0);



    }

    //******** Call function*********//

    void M_Matrix(float *Vi,float *Vr, float *S)

    {
    for(i=0;i<4;i++)
    {

    for(k=0;k<4;k++)
    {

    Vr[i]+= 0.5*(S[i][k]*Vi[k]); //***Error is here***//


    }
    }
    return;

    }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Newbie Hijack thread Alert.
    Learn to read and post correctly.
    Learn to use Code Tags
    Learn to read the sticky notes at top of forum that give directions.
    Learn NOT to open up old threads (greater that 3 months old with only slightly related post)
    Learn to write readable code.
    Learn to never define one character Macros.

    Tim S.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Learn to pass parameters correctly; learn to never use a global variable name as a local parameter and learn to never use a one letter global variable.

    Learn how to pass an 2 dimension array in C First Google result http://www.dfstermole.net/OAC/harray2.html

    Code:
    void M_Matrix(float *Vi,float *Vr, float *S)
    Last edited by stahta01; 12-11-2010 at 12:46 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    @Aldabbagh - start a NEW thread with code tags.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 6
    Last Post: 04-12-2002, 08:33 AM