0

I have this method that returns a value used in matrix triangularization routine

 float **trian(int n, float **Xy)
    {
        int i,row,col;
        float **sign = 1;
        for ( i = 0; i < n; i++) {
            int max = 0;

            for ( row = i; row < n; row++)
                if (fabs(Xy[row][i]) > fabs(Xy[max][i]))
                    max = row;

            if (max) {
                sign = -sign;
                float *tmp = Xy[i];
                Xy[i] = Xy[max], Xy[max] = tmp;
            }

            if (!Xy[i][i]) return 0;
            row=0;
            for (row = i + 1; row < n; row++) {
                float r = Xy[row][i] / Xy[i][i];
                if (!r) continue;

                for ( col = i; col < n; col ++)
                    Xy[row][col] -= Xy[i][col] * r;
            }
        }
        return sign;
    }

Unfortunately get this error

main.c:74:20: error: wrong type argument to unary minus

the line of the error

 sign = -sign;
AndreaF
  • 11,975
  • 27
  • 102
  • 168
  • can you please mark the line in the snippet? – Wolf May 22 '14 at 16:02
  • I suspect that here is already an error `float **sign = 1;` why does the compiler not complain? – Wolf May 22 '14 at 16:05
  • Why has it to be C? Speed? ;) ...seems you never used C before. – Wolf May 22 '14 at 16:10
  • As to return a pointer to a pointer to a float you have to use more variables, but these must not be local. Did you try to solve the problem in Java? – Wolf May 22 '14 at 16:13
  • Does [this code](http://stackoverflow.com/q/23744291/2932052) work as expected? Then it's only a porting problem. But: Why then **this** new question? Why not: "how to return an 2D array in C?" ? – Wolf May 22 '14 at 16:40

1 Answers1

1

there's something wrong here:

float **sign = 1;
....
sign = -sign;

did you mean to use **sign instead of sign?


perhaps you need just something like this:

int trian(int n, float **Xy) {
  ...
  int sign = 1;
  ...
  sign = -sign;
  ...
  return sign;
}
Pavel
  • 7,436
  • 2
  • 29
  • 42
  • yes the error is at this line, but if I write `float **sign = 1;` get `incompatible types error`. a way tofix this? – AndreaF May 22 '14 at 16:06
  • Is correct if I change `float **sign = 1` in `float sign = 1` and `return sign;` in `return **sign;`? – AndreaF May 22 '14 at 16:09
  • @AndreaF no. absolutely not. Better try another language. Python could be your friend. – Wolf May 22 '14 at 16:11
  • @Wolf I need C now not python. I have always used Java I'm new to C language. I need to calc sign and return the reference to sign value. – AndreaF May 22 '14 at 16:22
  • so what exactly is `sign` supposed to be? a `float`? or a matrix? or is it just `-1`/`1` and you can go with `int`? – Pavel May 22 '14 at 16:23
  • @AndreaF why do you return a reference to sign? – Wolf May 22 '14 at 16:26
  • @Pavel It's a value not a matrix, yes I should go also with int – AndreaF May 22 '14 at 16:27
  • @wolf Is there a precise reason why I have to return the value and not the reference? – AndreaF May 22 '14 at 16:31
  • @AndreaF you may not return a reference to a local variable, because otherwise you cause undefined behaviour (wrong values, crashes) when accessing the reference after the function has returned. Values are fine, they are copied to the caller (who holds the memory for it). – Wolf May 22 '14 at 16:34