Array subscription

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mingke
    New Member
    • Dec 2008
    • 16

    Array subscription

    Hi....

    I have a problem with my declaring my array. I just keep getting message like this:
    invalid types `float[9][float]' for array subscript

    Here my code:
    Code:
    #define size 2
    #define size2 size*size
    #define point2 (size+1)*(size+1)
    int main ( )
    {
    float ax[size2][24],ay[size2][24]; 
    float R[size2][point2],S[size2][24][point2]; 
    float e;
    for (i=0; i<size2; i++){
         for (l=0;l<24;l++){
              for (j = 0; i < Lx[i]; j++){
    		for (k = 0; k < Ly[i]; k++){
                        e=j+k*Lx[i];
                         R[i][l][e]=ax[i][l];
                          S[i][l][e]=ay[i][l];
                        }
                  }
            }
    }
    }
    I don't know what's wrong..
    if you could help me, I really appreciate it

    Thank you
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What is Lx? It's not in your snippet.

    Comment

    • mingke
      New Member
      • Dec 2008
      • 16

      #3
      Sorry...
      I cut my code because it is so long...
      Here my complete code (although I cut a few part)
      Code:
      #include <stdlib.h>
      #include <math.h>
      #include <stdio.h>
      #include <conio.h>
      
      #define size 2
      #define size2 size*size
      #define point1 (size+1)
      #define point2 (size+1)*(size+1)
      #define rotation 45
      #define deg rotation*3.14159265/180
      
      int main()
      {
      float xr[point2], yr[point2];
      float xb[point2], yb[point2];
      float bx[size2][4],by[size2][4];  
      float bx_max[size2],bx_min[size2],by_max[size2],by_min[size2];        
      float Lx1[size],Lx2[size],Ly1[size],Ly2[size];  
      float Lx[size],Ly[size]; 
      float R[size2][point2],S[size2][24][point2]; 
      int i,j,k,l,flag1,flag2;
      int a,d;
      
      	for (i = 0; i < point1; i++){
      		for (j = 0; j < point1; j++){
         		xr[j+i*point1] = -(size/2) + j;
         		yr[j+i*point1] = (size/2) - i;
         	   }
      	}
          for (i = 0; i < point2; i++){
       		xb[i] = xr[i]*cos(deg) - yr[i]*sin(deg);
         		yb[i] = xr[i]*sin(deg) + yr[i]*cos(deg);
          }
      for (i = 0; i < size; i++){
        for (j = 0; j < size; j++){            
           a=j+i*size;
           by[a][0]=xr[a+i]*sin(deg) + yr[a+i]*cos(deg);    
           by[a][1]=xr[a+1+i]*sin(deg) + yr[a+1+i]*cos(deg);   
           by[a][2]=xr[a+i+point1]*sin(deg) + yr[a+i+point1]*cos(deg);
           by[a][3]=xr[a+i+1+point1]*sin(deg) + yr[a+i+1+point1]*cos(deg);
           bx[j+i*size][0]=xb[j+i*size+i];
           bx[j+i*size][1]=xb[a+i+1]; 
           bx[j+i*size][2]=xb[a+i+point1];                       
           bx[j+i*size][3]=xb[1+i+a+point1];
         }
      }
      for (i=0; i<size2; i++){
        	    for (j = 0; j < 4; j++){
                  bx_max[i]=bx[i][0];
                      if (bx[i][j]>bx_max[i]){
                          bx_max[i]=bx[i][j];
                      }
                  by_max[i]=by[i][1];
                      if (by[i][j]>by_max[i]){
                          by_max[i]=by[i][j];
                      }
             }
        }               
         for (i=0; i<size2; i++){
        	    for (j = 0; j < 4; j++){
                  bx_min[i]=bx[i][0];
                  if (bx[i][j]<bx_min[i]){
                      bx_min[i]=bx[i][j];
                  }
                 // by_min[i]=by[i][0];  
                  if (by[i][j]<by_min[i]){
                      by_min[i]=by[i][j];
                  }
             }
         }
      for (i=0; i<size2; i++){
             Lx1[i]=ceil (bx_max[i]);
             Lx2[i]=floor (bx_min[i]);
             Ly1[i]=ceil (by_max[i]);
             Ly2[i]=floor(by_min[i]);
             Lx[i]=Lx1[i]-Lx2[i];
             Ly[i]=Ly1[i]-Ly2[i];
        }
      for (i=0; i<size2; i++){
            for (l=0;l<24;l++){
              for (j = 0; i < Lx[i]; j++){
                for (k = 0; k < Ly[i]; k++){
                     e=j+k*Lx[i];
                     R[i][l][e]=ax[i][l];
                     S[i][l][e]=ay[i][l];
                 }
             }
          }
       }
      
      }
      while ax and ay are values that have been stated before. I'm sorry I didn't put it in my code because it's so long.
      And the my problem is it's said that I have invalid types `float[9][float]' for array subscript (R[i][l][e] and S[i][l][e])


      Thank you

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        declaration: float R[size2][point2]
        usage: R[i][l][e]
        Decalration contains 2 indexes, and you try to index it with 3 indexes.

        Comment

        • mingke
          New Member
          • Dec 2008
          • 16

          #5
          @newb16

          thanks for your respond....

          But how about S[i][l][e]?
          I declared it with three index and use it with three index too....
          Same things happen if I declared R with three index...

          Is it possible because e contains Lx[i] and somehow I declared it wrong?

          Comment

          • newb16
            Contributor
            • Jul 2008
            • 687

            #6
            Maybe you can't use float as array index, but only integral type. I tried to compile the code you posted, but got an 'e was not declared in this scope' error at line 84.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Why are floats being used at all?

              I have always recommended that unless you can write down on paper the precise technical reason you need a float you should double instead.

              You lose accuracy converting from double to float.

              Next, array indexes are integers. When you use a float or a double to calculate an index, all of the integers are converted to double and the calculation is done as a double. Then since array indexes are integers, the resultant double is converted to an int by chopping off the decimals and since a double can hold a bigger value than an int, maybe some of the lower digits are chopped off as well.

              You cannot convert a double to an int.

              Before I can go further, I would need code that compiles without warnings.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by weaknessforcats
                Next, array indexes are integers. When you use a float or a double to calculate an index, all of the integers are converted to double and the calculation is done as a double.
                It's more dramatic than that: if you use floats or doubles for an index expression the code violates a 'shall clause'. This is what the C Standard says:

                Originally posted by C Standard
                3.3.2.1 Array subscripting

                Constraints

                One of the expressions shall have type "pointer to object type ,''
                the other expression shall have integral type, and the result has type
                "type .''
                Violating a shall clause is a crime: error messages must be given and the author of the violation must be defenestrated. This is what the C Standard says about those 'shall clauses':

                Originally posted by C Standard
                In this Standard, "shall'' is to be interpreted as a requirement
                on an implementation or on a program; conversely, "shall not'' is to
                be interpreted as a prohibition.
                kind regards,

                Jos

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  It can be more surprising than you expect to invoke "undefined behavior" or "implementa tion-defined behavior" in your C program.

                  I don't know if this is just an urban legend, but the word is that the implementation-defined reaction of early versions of gcc was to launch a unix game such as NetHack or Rogue whenever you compiled a program containing #pragma.

                  The only difference between "undefined behavior" and "implementa tion-defined behavior" is that the compiler has to document what happens in the latter case. Other than that, anything goes. It would be legal from a C Standard point-of-view to reformat your hard drive.

                  Defenestration is restrained behavior.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by donbock
                    I don't know if this is just an urban legend, but the word is that the implementation-defined reaction of early versions of gcc was to launch a unix game such as NetHack or Rogue whenever you compiled a program containing #pragma.
                    It is true, I saw the sources: when the cppp saw a #pragma it fired up a game. (I forgot which one). It was a single file mess of a program; later versions of the preprocessor have cleaned up a lot.

                    kind regards,

                    Jos

                    Comment

                    Working...