Array subscript is not an integer ошибка

following this previous question Malloc Memory Corruption in C, now i have another problem.
I have the same code. Now I am trying to multiply the values contained in the arrays A * vc
and store in res. Then A is set to zero and i do a second multiplication with res and vc and i store the values in A. (A and Q are square matrices and mc and vc are N lines two columns matrices or arrays).
Here is my code :

    int jacobi_gpu(double A[], double Q[], 
           double tol, long int dim){
  int nrot, p, q, k, tid;
  double c, s;
  double *mc, *vc, *res;
  int i,kc;
  double vc1, vc2;

  mc   = (double *)malloc(2 * dim * sizeof(double));
  vc   = (double *)malloc(2 * dim * sizeof(double));
  vc   = (double *)malloc(dim * dim * sizeof(double));

  if( mc == NULL || vc == NULL){
    fprintf(stderr, "pb allocation matricren");
    exit(1);
  }

  nrot = 0;

  for(k = 0; k < dim - 1; k++){

    eye(mc, dim);
    eye(vc, dim);

    for(tid = 0; tid < floor(dim /2); tid++){
      p = (tid + k)%(dim - 1);
      if(tid != 0)
    q = (dim - tid + k - 1)%(dim - 1);
      else
    q = dim - 1;

      printf("p = %d | q = %dn", p, q);
      if(fabs(A[p + q*dim]) > tol){

    nrot++;
    symschur2(A, dim, p, q, &c, &s);


    mc[2*tid] = p;                                               vc[2 * tid] = c;
    mc[2*tid + 1] = q;                                           vc[2*tid + 1] = -s;
    mc[2*tid + 2*(dim - 2*tid) - 2] = p;                         vc[2*tid + 2*(dim - 2*tid)   - 2 ] = s;
    mc[2*tid + 2*(dim - 2*tid) - 1] = q;                         vc[2 * tid + 2*(dim - 2*tid) - 1 ] = c;



    }
    }

    for( i = 0; i< dim; i++){
      for(kc=0; kc < dim; kc++){
    if( kc < floor(dim/2)) {
      vc1 = vc[2*kc + i*dim];
      vc2 = vc[2*kc + 2*(dim - 2*kc) - 2];
    }else {
      vc1 = vc[2*kc+1 + i*dim];
      vc2 = vc[2*kc - 2*(dim - 2*kc) - 1];
    }
    res[kc + i*dim] = A[mc[2*kc] + i*dim]*vc1 + A[mc[2*kc + 1] + i*dim]*vc2;
      }
    }

    zero(A, dim);

    for( i = 0; i< dim; i++){
      for(kc=0; kc < dim; k++){
    if( k < floor(dim/2)){
      vc1 = vc[2*kc + i*dim];
      vc2 = vc[2*kc + 2*(dim - 2*kc) - 2];
    }else {
      vc1 = vc[2*kc+1 + i*dim];
      vc2 = vc[2*kc - 2*(dim - 2*kc) - 1];
    }
    A[kc + i*dim] = res[mc[2*kc] + i*dim]*vc1 + res[mc[2*kc + 1] + i*dim]*vc2;
      }
    }


    affiche(mc,dim,2,"Matrice creuse");
    affiche(vc,dim,2,"Valeur creuse");

  }

  free(mc);
  free(vc);
  free(res);
  return nrot;
}

When i try to compile, i have this error :

    jacobi_gpu.c: In function ‘jacobi_gpu’:
jacobi_gpu.c:103: error: array subscript is not an integer
jacobi_gpu.c:103: error: array subscript is not an integer
jacobi_gpu.c:118: error: array subscript is not an integer
jacobi_gpu.c:118: error: array subscript is not an integer
make: *** [jacobi_gpu.o] Erreur 1

The corresponding lines are where I store the results in res and A :

res[kc + i*dim] = A[mc[2*kc] + i*dim]*vc1 + A[mc[2*kc + 1] + i*dim]*vc2;

and

A[kc + i*dim] = res[mc[2*kc] + i*dim]*vc1 + res[mc[2*kc + 1] + i*dim]*vc2;

Can someone explain me what is this error and how can i correct it?
Thanks for your help. ;)

C11dr 6.5.2.1 Array subscripting … «One of the expressions shall have type ‘‘pointer to complete object type’’, the other expression shall have integer type, and the result has type ‘‘type’’.» …

With [], you get to do:

// pointer_type[integer_type]
float data[];
int n;
float total;
total = data[n];

// or the unconventional equivalent
// integer_type[pointer_type]
total = n[data];  // But let's leave that for another post

// Instead the OP did
// pointer_type[pointer_type]
// and received error: "Array subscript is not an integer"
float *p;
total = data[p];

The usage of float is not the issue here, but the usage of a pointer.

An integer type includes types int, unsigned, size_t, long, etc.


I think the OP wanted the following (or something like this)

float deviation(float data[], int n) {
  if (i <= 0) return 0;
  float data_average = average(data, n);
  float total = 0.0;  // don't forget to set to 0.0
  float *p = data;

  while (p < (data + n)) {
    total += (*p - data_average) * (*p - data_average);
    p++;
    }
  return sqrt(total / n);  // div by 0 averted
}


Schol-R-LEA

1,446



Commie Mutant Traitor



Featured Poster


9 Years Ago

in the line in question:

 sum[M][3] = inputarray[M][3] + inputarray2[M][3];

You are treating inputarray() and inputarray2() as arrays, rather than calling them as functions. The correct syntax would be:

 sum[M][3] = inputarray(M, 3) + inputarray2(M, 3);

BTW, I strongly recommend you get into the practice of indenting your code suitably. The usual practice is to indent one level — three to eight spaces, depending on your indent style — every time you have a block of code that is inside a loop or an if statement. This practice, called ‘nesting’, is very important. The goal of formatting code is to make the nesting levels explicit in the layout of the program. While in C, it is purely for the benefit of the programmer, it should make reading and editing the code easier, which is why it is important to get into the habit as soon as you can. I recommend reading this article on the subject for further clarification.

Now, as this Wikipedia entry explains, there are several different styles which one can use to indent their code; what is important is not the specific style, but consistency in applying your chosen style. As long as you follow the main rule — indent inside a block, de-dent after the end of a block — you should be able to use the style you are comfortable with, **so long as you are consistent in appyling it*. those two rules are the real key; as long as your code makes the nesting express, and you don’t change styles in the middle of a program, your good.

Fortunately, there exist several editors and styling programs that will format code automatically for you. I recommend AStyle as a simple one you can download and run on your computer. By default, it formats to Allman Style with an indent of four, but it is configurable to several different styles. If you download Code::Blocks, it includes AStyle as a plugin which can be called from the editor.

Here is your code agin, but formatted with AStyle:

#include <stdio.h>

void inputarray(int arg[][3], int rows);
void printarray (int arg[][3], int rows);
void inputarray2(int arg[][3], int rows);
void printarray2(int arg[][3], int rows);
int main ()
{
    int M[3][3];
    int action, sum=0;
    inputarray(M,3);
    printarray(M,3);
    inputarray2(M,3);
    printarray2(M,3);

    printf("1:Add the matrices");
    printf("2:Subtract the matrices");
    printf("3:Multiply the matrices");
    printf("4:EXIT");
    printf("Please enter your choice(1-4): ");
    scanf("%d", &action);

    switch(action) {
    case 1: {
        sum[M][3] = inputarray(M, 3) + inputarray2(M, 3);

        printf("Sum of entered matrices:-n");
        printf("%d", sum[M][3]);
        printf("n");
        break;
    }
    }



    return 1;
}
void inputarray(int arg[][3], int rows)
{
    int row,col;
    for(row=0; row<rows; row++)
    {
        printf("3 numbers for Row %d for first matrix : ",(row+1));
        for(col=0; col<3; col++)
            scanf("%d",&arg[row][col]);
    }
}
void printarray (int arg[][3], int rows)
{
    int x, y;
    for (x=0; x<rows; x++)
    {
        for(y=0; y<3; y++)
            printf("%dt",arg[x][y]);
        printf("n");
    }
    printf("n");
}
void inputarray2(int arg[][3], int rows)
{
    int row,col;
    for(row=0; row<rows; row++)
    {
        printf("3 numbers for Row %d of second matrix : ",(row+1));
        for(col=0; col<3; col++)
            scanf("%d",&arg[row][col]);
    }
}
void printarray2 (int arg[][3], int rows)
{
    int x, y;
    for (x=0; x<rows; x++)
    {
        for(y=0; y<3; y++)
            printf("%dt",arg[x][y]);
        printf("n");
    }
    printf("n");
}

Edited

9 Years Ago
by Schol-R-LEA

  1. 11-06-2012


    #1

    prathiksa is offline


    Registered User


    array subscript is not an integer

    im getting the error when compiling the code…

    Code:

    float array[20];
    int last;
    array[last[array]];
    how to get red of this


  2. 11-06-2012


    #2

    std10093 is offline


    SAMARAS

    std10093's Avatar


    You have

    Code:

    array[last[array]];

    In general,if you are sure you want something like this,imagine it with numbers and arrays.It would be something like this
    if you want to access the index described by the last element of the array.
    So maybe you want to say


  3. 11-06-2012


    #3

    prathiksa is offline


    Registered User


    still the same error,i think may be its due to types of the integer variable and float arrays


  4. 11-06-2012


    #4

    std10093 is offline


    SAMARAS

    std10093's Avatar


    Have you initialized variable last? Can you please post the code you complied?


  5. 11-06-2012


    #5

    prathiksa is offline


    Registered User


    im just checking the syntax errors
    i have initialised int last = n-1;
    int *l ;
    l = &last;


  6. 11-06-2012


    #6

    std10093 is offline


    SAMARAS

    std10093's Avatar


    What is n?Where is the array you were talking about? :P
    Just post all the code you have (i consider it is small from what you say )
    Also it would be very kind of you if you could wrap your code in code tags.It is easy
    [key]/*Your code here*/[/key] Replace key with code in order this to work.

    EDIT : it is ok to say

    Code:

    int a=5;
    int* p;
    p=&a;

    Last edited by std10093; 11-06-2012 at 05:17 AM.


  7. 11-06-2012


    #7

    grumpy is offline


    Registered User


    Quote Originally Posted by prathiksa
    View Post

    still the same error,i think may be its due to types of the integer variable and float arrays

    Yes it is.

    Arrays can only be indexed using values that have integral type (char, int, long, unsigned, etc). They cannot be indexed using non-integral values (float as in your case, struct types, etc).

    There is also an obscure corner of C that causes «array[index]» and «index[array]» to be equivalent (assuming array is (well….) an array or a pointer, and index has integral type). Because of that, your expression «array[last[array]]» is equivalent to «array[array[last]]» which attempts to use array[last] as an index. In your code, array[last] is of type float so is not a valid index. Hence the compiler error.

    Right 98% of the time, and don’t care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.


  8. 11-06-2012


    #8

    std10093 is offline


    SAMARAS

    std10093's Avatar


    What i thought you wanted to do

    Code:

    #include <stdio.h>
    
    int main( int argc, char *argv[])
    {
      float array[5];
      int last = 4;
      array[0]=1.2;
      array[1]=1.3;
      array[2]=1.4;
      array[3]=1.5;
      array[4]=1.9;
    
      printf("%fn",array[last]);
    
      return(0);
    }

    grumpy is more experienced,so he is sure to have it better in his mind


  9. 11-06-2012


    #9

    iMalc is offline


    Algorithm Dissector

    iMalc's Avatar


    You get rid of the error by fixing or removing the nonsense code.
    Explain what you really want to do and we’ll show you how to write code for it that makes sense.

    My homepage
    Advice: Take only as directed — If symptoms persist, please see your debugger

    Linus Torvalds: «But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong»


p is a pointer to a float. Thats why youre getting the error.

float *p, total;

...

total += (datos[p]-prom)*(datos[p+1]-prom);

You can only use ints as array indices in C.

Array subscripts must be an integer, an int type. You cant use any other type as array subscript. p is declared as float *p, i.e, a pointer to float. You cant use a pointer as array indices.

Array subscript is not an integer in C program

You can use an int:

int p = 0;
...
while (p<n) {
    // rest is the same

or, you can use p as a pointer directly, which is, I suppose, what you intended.

while(p<datos+n) {
    total += (*p-prom)*(*(p+1)-prom);

Note, however, that you never increment p, so the loop will never end. Also, you never initialise total, so the result will be a random garbage value.

Понравилась статья? Поделить с друзьями:
  • Arma 3 ошибка 0xc0000007b
  • Arma 3 ошибка 0xc0000005
  • Arma 3 ошибка 0xc00000035
  • Arma 3 ошибка 0x00000035
  • Arma 3 выдает ошибку при запуске