Connecting Tech Pros Worldwide Help | Site Map

Is these functions C90 and/or C99 ?

Bernard
Guest
 
Posts: n/a
#1: Nov 14 '05
Hello,

I would like your advices on the following two functions which multiply
two arrays.

About Multiply_array, I think that this function is ISO C90, but for
Product_array I have some doubts...
You can see that Product_array try to use the new advantages of C99
(Variable Length Array) but still I'm a little surprised that this
code can compile and run flawlessly (with gcc 3.2).

I notice that if I use the new function parameters definition :

void Product_array(double mat_e1[dim][dim], double mat_e2[dim][dim],
double mat_s[dim][dim], int dim)
It doesn't even compile !

So here are my questions :
1) Can you tell me if Multiply_array is ISO C90 ?
2) Can you tell me if Product_array is ISO C99 and tell me why it works
? (for instance if it's just a new function of the C99 ?)

Thanks in advance and here is the code :

#include <stdio.h>
#include <stdlib.h>

#define DIMENSION 10

void Product_array (mat_e1, mat_e2, mat_s, dim)
int dim;
double mat_e1[dim][dim], mat_e2[dim][dim], mat_s[dim][dim];
{
int i, j, k;

for (i = 0; i < dim; i++)
{
for (j = 0; j < dim; j++)
{
mat_s[i][j] = 0.;
for (k = 0; k < dim; k++)
{
mat_s[i][j] = mat_s[i][j] + mat_e1[i][k] * mat_e2[k][j];
}
}
}
}

void Multiply_array (void *A, void *B, int Nblignes, int
Nbcolonnes, int DimMultiplication, void *X)
{
int i, j, k;
double produit;
double *Matrice_A = A;
double *Matrice_B = B;
double *Matrice_X = X;


for (i = 0; i < Nblignes; i++)
for (j = 0; j < Nbcolonnes; j++)
{
produit = 0;
for (k = 0; k < DimMultiplication; k++)
produit =
produit + Matrice_A[i * DimMultiplication +
k] * Matrice_B[k * Nbcolonnes + j];
Matrice_X[i * Nbcolonnes + j] = produit;
}
}


int main (void)
{
double A[DIMENSION][DIMENSION];
double B[DIMENSION][DIMENSION];
double C[DIMENSION][DIMENSION];

int i, j;

for (i = 0; i < DIMENSION; i++)
for (j = 0; j < DIMENSION; j++)
if (i == j)
A[i][j] = 1;
else
A[i][j] = 0;

for (i = 0; i < DIMENSION; i++)
for (j = 0; j < DIMENSION; j++)
B[i][j] = i + j;

Product_array (A, B, C, DIMENSION);
/*Multiply_array(A, B, DIMENSION, DIMENSION, DIMENSION, C); */

for (i = 0; i < DIMENSION; i++)
{
for (j = 0; j < DIMENSION; j++)
printf ("%f ", C[i][j]);
printf ("\n");
}

return EXIT_SUCCESS;
}
E. Robert Tisdale
Guest
 
Posts: n/a
#2: Nov 14 '05

re: Is these functions C90 and/or C99 ?


Bernard wrote:[color=blue]
>
>
> I would like your advices on the following two functions which multiply
> two arrays.
>
> About Multiply_array, I think that this function is ISO C90, but for
> Product_array I have some doubts...
> You can see that Product_array try to use the new advantages of C99
> (Variable Length Array) but still I'm a little surprised that this
> code can compile and run flawlessly (with gcc 3.2).
>
> I notice that if I use the new function parameters definition :
>
> void Product_array(double mat_e1[dim][dim], double mat_e2[dim][dim],
> double mat_s[dim][dim], int dim)
> It doesn't even compile !
>
> So here are my questions :
> 1) Can you tell me if Multiply_array is ISO C90 ?
> 2) Can you tell me if Product_array is ISO C99 and tell me why it works
> ? (for instance if it's just a new function of the C99 ?)
>
> Thanks in advance and here is the code :
>
> #include <stdio.h>
> #include <stdlib.h>
>
> #define DIMENSION 10
>
> void Product_array (mat_e1, mat_e2, mat_s, dim)
> int dim;
> double mat_e1[dim][dim], mat_e2[dim][dim], mat_s[dim][dim];
> {
> int i, j, k;
>
> for (i = 0; i < dim; i++)
> {
> for (j = 0; j < dim; j++)
> {
> mat_s[i][j] = 0.;
> for (k = 0; k < dim; k++)
> {
> mat_s[i][j] = mat_s[i][j] + mat_e1[i][k] * mat_e2[k][j];
> }
> }
> }
> }
>
> void Multiply_array (void *A, void *B, int Nblignes, int
> Nbcolonnes, int DimMultiplication, void *X)
> {
> int i, j, k;
> double produit;
> double *Matrice_A = A;
> double *Matrice_B = B;
> double *Matrice_X = X;
>
>
> for (i = 0; i < Nblignes; i++)
> for (j = 0; j < Nbcolonnes; j++)
> {
> produit = 0;
> for (k = 0; k < DimMultiplication; k++)
> produit =
> produit + Matrice_A[i * DimMultiplication +
> k] * Matrice_B[k * Nbcolonnes + j];
> Matrice_X[i * Nbcolonnes + j] = produit;
> }
> }
>
>
> int main (void)
> {
> double A[DIMENSION][DIMENSION];
> double B[DIMENSION][DIMENSION];
> double C[DIMENSION][DIMENSION];
>
> int i, j;
>
> for (i = 0; i < DIMENSION; i++)
> for (j = 0; j < DIMENSION; j++)
> if (i == j)
> A[i][j] = 1;
> else
> A[i][j] = 0;
>
> for (i = 0; i < DIMENSION; i++)
> for (j = 0; j < DIMENSION; j++)
> B[i][j] = i + j;
>
> Product_array (A, B, C, DIMENSION);
> /*Multiply_array(A, B, DIMENSION, DIMENSION, DIMENSION, C); */
>
> for (i = 0; i < DIMENSION; i++)
> {
> for (j = 0; j < DIMENSION; j++)
> printf ("%f ", C[i][j]);
> printf ("\n");
> }
>
> return EXIT_SUCCESS;
> }[/color]
[color=blue]
> gcc -Wall -std=c89 -pedantic -o main main.c[/color]
main.c: In function `Product_array':
main.c:8: warning: ISO C90 forbids variable-size array `mat_e1'
main.c:8: warning: ISO C90 forbids variable-size array `mat_e1'
main.c:8: warning: ISO C90 forbids variable-size array `mat_e2'
main.c:8: warning: ISO C90 forbids variable-size array `mat_e2'
main.c:8: warning: ISO C90 forbids variable-size array `mat_s'
main.c:8: warning: ISO C90 forbids variable-size array `mat_s'[color=blue]
> gcc -Wall -std=c99 -pedantic -o main main.c
>[/color]

Evidently, it is a valid C99 program but not a valid C89 program.
Bernard
Guest
 
Posts: n/a
#3: Nov 14 '05

re: Is these functions C90 and/or C99 ?


On Wed, 23 Jun 2004 15:15:59 -0700, "E. Robert Tisdale"
<E.Robert.Tisdale@jpl.nasa.gov> wrote :
[color=blue][color=green]
> > I notice that if I use the new function parameters definition :
> >
> > void Product_array(double mat_e1[dim][dim], double mat_e2[dim][dim],
> >
> > double mat_s[dim][dim], int dim)
> > It doesn't even compile ![/color][/color]
[color=blue]
> Evidently, it is a valid C99 program but not a valid C89 program.[/color]

Thanks for your comments.
But if it is a valid C99 program, why does it only work if I use the old
style function parameters declaration ?

I'm aware that the following declaration is correct :
void Product_array(int dim, double mat_e1[dim][dim], double
mat_e2[dim][dim], double mat_s[dim][dim])

But why is the previous declaration wrong ?
I thought that the old-style and new-style functions parameters
declaration was the same ?!?
E. Robert Tisdale
Guest
 
Posts: n/a
#4: Nov 14 '05

re: Is these functions C90 and/or C99 ?


Bernard wrote:
[color=blue]
> E. Robert Tisdale wrote :
>[color=green][color=darkred]
>>>I notice that if I use the new function parameters definition:
>>>
>>>void Product_array(double mat_e1[dim][dim], double mat_e2[dim][dim],
>>> double mat_s[dim][dim], int dim)
>>>
>>>It doesn't even compile![/color][/color]
>[color=green]
>>Evidently, it is a valid C99 program but not a valid C89 program.[/color]
>
> But, if it is a valid C99 program, why does it only work
> if I use the old style function parameters declaration?[/color]

Evidently, the compiler needs to parse the type declaration of int dim
before is can use it to parse the dimensions of the arrays.
[color=blue]
> I'm aware that the following declaration is correct:
> void Product_array(int dim, double mat_e1[dim][dim], double
> mat_e2[dim][dim], double mat_s[dim][dim])
>
> But why is the previous declaration wrong?
> I thought that the old-style and new-style functions parameters
> declaration was the same?[/color]

This is what you wrote:

void Product_array(mat_e1, mat_e2, mat_s, dim)
int dim;
double mat_e1[dim][dim], mat_e2[dim][dim], mat_s[dim][dim];

Note that the type declaration of int dim is parsed before it is used
in the two-dimensional array type declarations.
Closed Thread