7

Was there a very early C "standard" where the following was legal for the definition of a two-dimensional array?

int array[const_x, const_y];
int array2[2, 10];

I've stumbled upon some old legacy code which uses this (and only this) notation for multi-dimensional arrays. The code is, except for this oddity, perfectly valid C (and surprisingly well-designed for the time).

As I didn't find any macros which convert between [,] and [][], and I assume it's not a form of practical joke, it seems that once upon a time there hath been thy olde C compiler which accepted this notation. Or did I miss something?

Edit: If it helps, it's for embedded microcontrollers (atmel). From experience I can tell, that embedded compilers are not that well-known for standard-compliance.

The code on current compilers works as intended (as far as it can be guessed from the function names, descriptions and variables) if I change all [,] to [][].

Karthik T
  • 31,456
  • 5
  • 68
  • 87
vsz
  • 4,811
  • 7
  • 41
  • 78
  • 4
    Are you sure it's compiled as multi-dimensional arrays? Maybe comma-separated expressions? So it's actually `int array[const_y]` ?... – Xiao Jia Jan 14 '13 at 04:52
  • btw, can you compile it with a modern compiler? – Xiao Jia Jan 14 '13 at 04:53
  • How old is the legacy code? – Karthik T Jan 14 '13 at 04:55
  • @XiaoJia: If I change all `[,]` to `[][]` it compiles, and it works as intended. There look like they are intended as 2-dimensional arrays, as it is used for those variables throughout the whole code, and those variables, from their names and usage, seem to be intended to be two-dimensional arrays. – vsz Jan 14 '13 at 04:58
  • @KarthikT: Hard to know when they started, but there were changes committed in the mid 00's so it seems that old compiler was still used in the development. – vsz Jan 14 '13 at 04:59
  • 1
    Do you know what compiler or hardware platform the code was written for? – Potatoswatter Jan 14 '13 at 05:07
  • @Potatoswatter: for Atmel microcontrollers. Their first processor came out in 1993, so it's not older than that (except if ported from something older) – vsz Jan 14 '13 at 05:31
  • 1
    @vsz Nevertheless, unless the guys were both quite clueless and extremely lucky the Atmel compiler must have supported the supposed extension. According to Wikipedia the first Atmel MCU in 1993 used the 8051 instructions, so there are plenty of obscure compilers to check out. But as for AVR, GCC is it, and it certainly won't do that. – Potatoswatter Jan 14 '13 at 06:35

2 Answers2

5

Take a look at this forum post.

the comma operator evaluates the left hand side, discards the result, then evaluates the right hand side. Thus "2,5" is the same as "5", and "5,2" is the same as "2".

This could be what is happening, although the why of it is beyond me.

Note that comma cannot be used in indexing multidimensional array: the code A[i, j] evaluates to A[j] with the i discarded, instead of the correct A[i][j]. This differs from the syntax in Pascal, where A[i, j] is correct, and can be a source of errors.

From Wikipedia

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • 1
    Of course, this requires VLAs which AFAIK weren't in ancient C versions. – Pubby Jan 14 '13 at 04:54
  • 2
    @Pubby: Why would it require VLAs if it happens to be a constant expression? – jamesdlin Jan 14 '13 at 04:56
  • A constant expression is not the same as an expression involving const-qualified variables. GCC complains if you use `-pedantic` for the following code in C89 mode but compiles fine in C99 mode: `int main(){const int x = 5; char y[x]; return 0;}` – dreamlax Jan 14 '13 at 05:08
  • @jamesdlin: See this [answer](http://stackoverflow.com/a/3025106/10320) on what constant expressions are in C and how they differ fundamentally from C++. – dreamlax Jan 14 '13 at 05:12
  • I know about the comma operator, and it does not seem to be intended. From the flow and my understanding of the intended functionality, they are intended to be 2-dimensional arrays. If I change it to `[][]`, it works fine as understood from the names and descriptions of the functions. Now I found another program from the same group, and it has an array which is declared as `[][]` but used as `[,]`. – vsz Jan 14 '13 at 05:32
  • @dreamlax: Sure, I understand the difference, but who said anything about the expression involving `const`-qualified variables? `const_x` and `const_y` in the original question could be macros or `enum` constants. – jamesdlin Jan 14 '13 at 06:31
  • I don't know why this was downvoeted. It was answered before I pointed out in the comments, that they cannot be intentional comma operators. I did a +1 to offset it. – vsz Jan 14 '13 at 08:36
  • @jamesdlin: They *could* be, but I figured they were named `const_x` and `const_y` in the original question to indicate their qualifiers (otherwise they are potentially rather poorly named macros or enumerators). – dreamlax Jan 14 '13 at 08:38
  • @jamesdlin I named them as such to not clot this site with_long_names_that_have_nothing_to_do_with_this_question. What I presented is an example to illustrate the problem, not actual quoting from the code, as the variable names are irrelevant. – vsz Jan 14 '13 at 08:40
5

The first formal standard was ANSI X3.159-1989, and the first informal standard would generally be agreed to be the first edition of Kernighan & Ritchie. Neither of these allowed the comma to be used to declare a two-dimensional array.

It appears to be an idiosyncracy of your particular compiler (one that renders it non-standard-conforming, since it would change the semantics of some conforming programs).

caf
  • 233,326
  • 40
  • 323
  • 462