1

Compelled to use the variable length array feature for my auxiliary function that prints square matrices, I defined it as follows:

void print_matrix(M, dim)
     unsigned dim;
     int M[dim][dim];
{
    /* Print the matrix here. */
    ...

The good news is, the code works and has its parameters in the order I'd like them to be.

The bad news is, I had to use the "old-style" function declaration syntax in order to reference the yet-to-be-declared argument dim in the declaration of M, which is apparently considered obsolete and dangerous.

Is there a straightforward way to do the same with the "new-style" function declarations WITHOUT changing the order of the parameters? (And if not, is it considered acceptable use of the old-style syntax in this particular situation?)

  • 1
    You can't; you specify the dimension before the matrix: void print_matrix(unsigned dim, int (*M)[dim][dim]). You're printing a 3D array, of course. – Jonathan Leffler Mar 28 at 0:00
  • Perhaps you can use a void pointer in the parameter list and assign it to the correct pointer type in the function. – Ctx Mar 28 at 0:05
  • I suspect that if you specified -Wpedantic-errors, you'd get compilation errors. Certainly, K&R compilers did not support VLA notation. There's a moderate chance you're using GCC and it is providing extensions that allow it to work. – Jonathan Leffler Mar 28 at 0:08
  • @JonathanLeffler Both Clang(6.0.0) and GCC(7.3.0) issued no complaints about my code, even with -std=c11 -pedantic-errors, but great point about the compatibility, of course. Even if the standard does not explicitly prohibit it (or so it seems), it's probably going to be poorly tested on the GCC/Clang side and non-idiomatic. I'm just surprised you can do that at all, to be honest. – undercat Mar 28 at 0:17
  • I'm surprised too. At one level, it makes sense. I don't spend much time worrying about what compilers can do with extensions over K&R with K&R function definitions. I aim to work in portable C, and accept that I have to put the size parameters before the arrays that use them in the prototype notation. – Jonathan Leffler Mar 28 at 0:25
2

In portable (standard) C, you can't do what you show. You have to specify the dimension before the matrix. The original code in the question was:

void print_matrix(M, dim)
     unsigned dim;
     int (*M)[dim][dim];
{

and that can't be directly translated — it needs a prototype like this, with the dimension before the matrix:

void print_matrix(unsigned dim, int (*M)[dim][dim]);

This allows you to call the function with a 3D array. Or, with the revised notation in the question, you can print a 2D array:

void print_matrix(unsigned dim, int M[dim][dim]);

GCC provides an extension to assist. Quoting the manual:

If you want to pass the array first and the length afterward, you can use a forward declaration in the parameter list—another GNU extension.

struct entry
tester (int len; char data[len][len], int len)
{
  /* … */
}

You can write any number of such parameter forward declarations in the parameter list. They can be separated by commas or semicolons, but the last one must end with a semicolon, which is followed by the “real” parameter declarations. Each forward declaration must match a “real” declaration in parameter name and data type. ISO C99 does not support parameter forward declarations.

  • Using a combination of a prototype and an old-style declaration will avoid the need to throw long-standing argument-ordering conventions out the window. – supercat Apr 10 at 23:14
  • @supercat: what do you think the prototype looks like? The only ones that could work would be void print_matrix(int M[][*], unsigned dim) and void print_matrix(int M[*][*], unsigned dim). Interestingly, GCC seems to accept either notation, even with -pedantic; so does clang though with -Weverything it objects to the use of the VLA at all, simply on the grounds that it is a VLA ([-Werror,-Wvla]). […continued…] – Jonathan Leffler Apr 10 at 23:33
  • […continuation…] So, if you're prepared to write the function definition in the obsolescent style, using a feature added to the language after the style was declared obsolescent, then "yes, you can write the function definition K&R style and declare it with a prototype". I don't write functions K&R style any more, and I spend far too much time cleaning up a code base littered with them still (though the amount of litter has decreased dramatically recently — and bugs have been found). I won't ever advise anyone to write K&R style functions unless there is extreme duress applied to me. – Jonathan Leffler Apr 10 at 23:37
  • The prototypes using [*][*] are perfectly valid, and I'm not quite sure what objection you have to them. Things sometimes go from being obsolete to being necessary as a result of technological developments, and while there was never any reason other than standard-writer laziness for C99 to have made it necessary to use that syntax. In C89, the same caller-side semantics could be achieved with void test(double(*twodarr)[], int rows, int cols) {...} which could then cast the argument to a double* and perform pointer arithmetic on it as appropriate. – supercat Apr 11 at 14:14
  • Being able to have a called function use a 2D array without the extra casting step would be nice, but I don't see that as any justification for requiring that functions using the new syntax be called differently. Since compilers have no need to know or care about the array sizes in the parameter list until the entire list had been parsed, all that would be necessary would be to have the compiler buffer up the size arguments and then evaluate them after parsing the declarations, and that's trivial compared with some of the other gymnastics forced upon compilers by clumsy corner cases in C99. – supercat Apr 11 at 14:19
0

An old-style declaration which is preceded by a prototype is syntactically ugly, but is no more dangerous than a new style declaration.

void print_matrix(int M[*][*], unsigned dim);
void print_matrix(M, dim)
     unsigned dim;
     int M[dim][dim];
{
  ...
}

The authors of the Standard recognized that the old-style declarations were a valid, useful, and sufficient means of accomplishing the task, and decided that writing rules to allow with new-style declarations would be too much work, so they didn't want to bother. The combination of a prototype and old-style declaration is thus the only way to achieve the appropriate semantics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.