>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
The compiler didnt flag any errors since there is nothing syntacticaly wrong
with the statement.
The comma-operator is used to string several expressions together into one
expression. The comma-operator has a defined order of evaluation (as && and || also have) from left to right and the value of the resulting expression is the
right operand of the comma-operator.
Thus when writing A[n,m] it will perform like :
1, Evaluate n and throw this value away.
2, Evaluate m and use this value to index array A.
> 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.
when writing double A[n,m] you get access to the same element as if you had
written double A[m].
You can even write something like A[printf("Hello World\n"),m]; and it will
also be a valid C expression, although it might look quite weird.
>That is:
> double A[2,2];
>gave same result as
> double A[2];
> A[0,1] = some_value;
>gave same result as
> A[1,1] = some_value;
>and
> A[1] = some_value;
>Can somebody enlighten me in this matter? I couldn't find anything
>about it in any of the C books I have. If double A[n,m] is illegal,
>shouldn't I get a warning at least?
>Has it something to do with the comma operator, discarding the first
>value inside the braces?
>By the way, the compiler I used was Turbo C++ ver. 3.1, just in case