0

I am trying to make a function to calculate the transpose of a matrix of ints. The function takes the two dimensions of the matrix, those being d1 and d2, and the matrix itself, named arr, as well a a matrix called dest, which will be the transpose. Here's the code that I am trying to use:

void transpose(size_t d1, size_t d2, int arr[d1][d2], int dest[d2][d1])
{
    for (size_t i = 0; i < d1; i++)
    {
        for (size_t j = 0; j < d2; j++)
        {
            dest[j][i] = arr[i][j];
        }
    }
}

And here is what I get when I try to compile:

Exercicios\source\9.52.c:5:48: error: use of parameter outside function body before ']' token
 void transpose(size_t d1, size_t d2, int arr[d1][d2], int dest[d2][d1])
                                                ^
Exercicios\source\9.52.c:5:52: error: use of parameter outside function body before ']' token
 void transpose(size_t d1, size_t d2, int arr[d1][d2], int dest[d2][d1])
                                                    ^
Exercicios\source\9.52.c:5:53: error: expected ')' before ',' token
 void transpose(size_t d1, size_t d2, int arr[d1][d2], int dest[d2][d1])
               ~                                     ^
                                                     )
Exercicios\source\9.52.c:5:55: error: expected unqualified-id before 'int'
 void transpose(size_t d1, size_t d2, int arr[d1][d2], int dest[d2][d1])

Seeing this, one would probably say that I simply can't use the value of an argument in another argument, and it is what I thought the same, however, this function seems to work an online compiler and using the arguments as dimensions for other arguments was suggested on another question, as well as some articles here and there, so it must be (sort of) right. Even then, it does not work on my compiler(mingw-w64), so I was hoping anybody could help me figure this out.

  • What compiler are you using? I know that the Visual Studio (MSVC) `C` compiler will reject your code (it uses a very old `C`-language standard) but `clang` compiles your code, no problem. But I'm not sure what the minimum `C`-standard you need for such variable-length array arguments. Others here will know, though! – Adrian Mole Dec 08 '19 at 21:06
  • @Adrian-ReinstateMonica VLA support is mandatory in C99 and optional in C11. – kaylum Dec 08 '19 at 21:20
  • @kaylum Thanks for the clarification! So it seems that it came in 1999 and went away again in 2011? But, IIRC, MSVC *still* uses C90!! – Adrian Mole Dec 08 '19 at 21:24
  • @Adrian-ReinstateMonica VLAs didn't technically go away in C11. They just became optional. Many compilers like gcc support it even for C11 mode. – kaylum Dec 08 '19 at 21:35
  • @Adrian-ReinstateMonica I am using the latest release of mingw-w64, it it possible to do what I want in this compiler or do I need to use another one? – João Pedro Voga Dec 08 '19 at 21:56
  • Not sure - but other will be. However, if you're using a `c++` compiler, your code will fail *whatever* language standard you use (unless you have something with a *very* non-standard extension). – Adrian Mole Dec 08 '19 at 22:02
  • 1
    By mingw-w64, you mean GCC, do you? Try to add `-std=c99` or `-std=c11` to command line you use to compile your program. – Bob__ Dec 08 '19 at 22:19

0 Answers0