0

I have a code:

    int main(int argc, char **argv)
    {
      double A[600][4],  b[600], c[4] ;
      j = rand_lp(600, &(A[0][0]), &(b[0]), &(c[0]), &(result[0]));
    }
    int rand_lp(int n, double *A, double *b, double *c, double *result)
    {
      permutation(n, A, b);
      return 0;
    }
    void permutation(int n, double *A, double *b)
    {
      int i;
      int k;
      double tmp0, tmp1, tmp2, tmp3, tmpb;

      for(i = n; i > 0; i--)
      {
          k = rand()%i;

          tmp0 = A[i][0]; // error : subscripted value is neither array nor pointer nor vector
          tmp1 = A[i][1];
        tmp2 = A[i][2];
        tmp3 = A[i][3];
        tmpb = b[i];

        A[i][0] = A[k][0];
        A[i][1] = A[k][1];
        A[i][2] = A[k][2];
        A[i][3] = A[k][3];
        b[i] = b[k];

        A[k][0] = tmp0;
        A[k][1] = tmp1;
        A[k][2] = tmp2;
        A[k][3] = tmp3;
        b[k] = tmpb;
      }
   }

So de facto I need to shuffle a matrix, that's why I need access to all coefficents, by C doesn't suppot 2-dem arrays I read. I can't change the call and the arguments of the function "rand_lp". Any ideas?

Inisheer
  • 20,376
  • 9
  • 50
  • 82
user2970104
  • 41
  • 1
  • 7
  • You have *undefined behavior* because you use the arrays without initialization. – Some programmer dude Nov 24 '15 at 03:34
  • As for your problem, an array of `double` (like `b` or `c`) naturally decays to a pointer to `double`, but an array of arrays of `double` doesn't decay to that, it decays to a a pointer to arrays of `double`, i.e. `double (*a)[]`. – Some programmer dude Nov 24 '15 at 03:35
  • I'm sorry I didn't realize what to do to solve this problem – user2970104 Nov 24 '15 at 03:41
  • "C doesn't suppot 2-dem arrays I read." This is incorrect. `int a[10][10];` is a 2 dimensional array and totally legal in C. – Bobby Sacamano Nov 24 '15 at 04:21
  • This might be helpful http://stackoverflow.com/questions/6862813/c-passing-a-2d-array-as-a-function-argument – Bobby Sacamano Nov 24 '15 at 04:24
  • I can't change the call and the arguments of the function "rand_lp" – user2970104 Nov 24 '15 at 04:34
  • if the dimensions of the array are always the same, you could change the parameters of permutation. – Bobby Sacamano Nov 24 '15 at 04:36
  • @user2970104 in your function *definition*, `void permutation(int n, double *A, double *b)`, `double *A` should be `double (*A)[4]` or `double A[][4]` (both are equivalent, your choice). The change should be made in each function *declaration/definition* passing `A`. Note, when you call the functions in your code for use, you simply pass `A`, ( e.g. `permutation (n, A, b)`) – David C. Rankin Nov 24 '15 at 07:48
  • you can also use `double **A` if your array is done in the right way or encode N-dimensions to 1D array ... – Spektre Nov 25 '15 at 09:54

0 Answers0