home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: What is meant by: double A[n,m]?
- Message-ID: <1992Nov18.172026.21375@taumet.com>
- Organization: TauMetric Corporation
- References: <TORSTEIN.92Nov18125146@itekiris.kjemi.unit.no>
- Date: Wed, 18 Nov 1992 17:20:26 GMT
- Lines: 47
-
- torstein@itekiris.kjemi.unit.no (torstein hansen) writes:
-
- >As a newcomer to C, previously programming in Pascal, I used the
- >following declaration in one of my programs:
-
- >double A[n,m];
-
- >I assumed (erronously) that this would give a n by m matrix. But
- >obviously, it didn't. But the compiler never gave any errors or
- >warnings, and using
-
- > A[rownumber,colnumber]=some_value;
- >
- >was also accepted by the compiler. I'm not asking for the correct
- >way to define an array, I found that out quickly, but what is meant
- >by A[n,m]? It seems like the compiler just ignores the n and makes
- >it A[m]. This is also the case when assigning values to the array.
-
- Right. The declaration
- double A[n, m];
- is not strictly conforming C code, but many (most) compilers allow
- the comma-expression inside the brackets. The "n" is ignored and
- the declaration is equivalent to
- double A[m]
- as you surmised.
-
- The expression (as opposed to declaration)
- A[x, y]
- is perfectly legal, having a comma-expression inside the brackets.
- The 'x' on the left side of the comma is evaluated and the result is
- ignored. Except for side effects of evaluating 'x', the expression
- is equivalent to
- A[y]
-
- The difference is that a declaration of an array requires a constant-
- expression for the size, and a constant-expression may not contain a
- comma-expression. Many compilers allow it anyway, as long as the left
- side of the comma is a constant-expression.
-
- The C Standard is not absolutely clear on this point for declarations.
- In one place it forbids a comma-expression, and in another it says it
- is an allowable extension.
-
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-