Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is these functions C90 and/or C99 ?

Reply
Thread Tools

Is these functions C90 and/or C99 ?

 
 
Bernard
Guest
Posts: n/a
 
      06-23-2004
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;
}
 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      06-23-2004
Bernard wrote:
>
>
> 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;
> }


> gcc -Wall -std=c89 -pedantic -o main main.c

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'
> gcc -Wall -std=c99 -pedantic -o main main.c
>


Evidently, it is a valid C99 program but not a valid C89 program.
 
Reply With Quote
 
 
 
 
Bernard
Guest
Posts: n/a
 
      06-23-2004
On Wed, 23 Jun 2004 15:15:59 -0700, "E. Robert Tisdale"
<(E-Mail Removed)> wrote :

> > 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 !


> Evidently, it is a valid C99 program but not a valid C89 program.


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 ?!?
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      06-24-2004
Bernard wrote:

> E. Robert Tisdale wrote :
>
>>>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!

>
>>Evidently, it is a valid C99 program but not a valid C89 program.

>
> But, if it is a valid C99 program, why does it only work
> if I use the old style function parameters declaration?


Evidently, the compiler needs to parse the type declaration of int dim
before is can use it to parse the dimensions of the arrays.

> 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?


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Questions about C90 vs C99 dspfun C Programming 15 12-12-2007 12:01 PM
List of differences in headers between C90 and C99? Spiro Trikaliotis C Programming 6 03-30-2007 02:24 PM
C99 struct initialization (C99/gcc) jilerner@yahoo.com C Programming 3 02-20-2006 04:41 AM
size_t and C90/C99 Pedro Graca C Programming 7 02-12-2006 10:07 AM
Char difference between C90 and C99 Jason Curl C Programming 11 06-24-2005 03:26 AM



Advertisments