Is these functions C90 and/or C99 ?

Discussion in 'C Programming' started by Bernard, Jun 23, 2004.

  1. Bernard

    Bernard Guest

    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[j] = 0.;
    for (k = 0; k < dim; k++)
    {
    mat_s[j] = mat_s[j] + mat_e1[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[j] = 1;
    else
    A[j] = 0;

    for (i = 0; i < DIMENSION; i++)
    for (j = 0; j < DIMENSION; j++)
    B[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[j]);
    printf ("\n");
    }

    return EXIT_SUCCESS;
    }
     
    Bernard, Jun 23, 2004
    #1
    1. Advertising

  2. 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[j] = 0.;
    > for (k = 0; k < dim; k++)
    > {
    > mat_s[j] = mat_s[j] + mat_e1[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[j] = 1;
    > else
    > A[j] = 0;
    >
    > for (i = 0; i < DIMENSION; i++)
    > for (j = 0; j < DIMENSION; j++)
    > B[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[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.
     
    E. Robert Tisdale, Jun 23, 2004
    #2
    1. Advertising

  3. Bernard

    Bernard Guest

    On Wed, 23 Jun 2004 15:15:59 -0700, "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.


    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 ?!?
     
    Bernard, Jun 24, 2004
    #3
  4. 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.
     
    E. Robert Tisdale, Jun 24, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Curl

    Char difference between C90 and C99

    Jason Curl, Jun 21, 2005, in forum: C Programming
    Replies:
    11
    Views:
    805
  2. Pedro Graca

    size_t and C90/C99

    Pedro Graca, Feb 11, 2006, in forum: C Programming
    Replies:
    7
    Views:
    1,727
    Keith Thompson
    Feb 12, 2006
  3. Spiro Trikaliotis

    List of differences in headers between C90 and C99?

    Spiro Trikaliotis, Mar 29, 2007, in forum: C Programming
    Replies:
    6
    Views:
    631
    Spiro Trikaliotis
    Mar 30, 2007
  4. Replies:
    3
    Views:
    621
    Keith Thompson
    Mar 31, 2007
  5. dspfun

    Questions about C90 vs C99

    dspfun, Dec 10, 2007, in forum: C Programming
    Replies:
    15
    Views:
    2,224
    Nick Keighley
    Dec 12, 2007
Loading...

Share This Page