Conformant arrays-- how to?

Doug Schmidt schmidt at zola.ics.uci.edu
Tue May 2 12:54:09 AEST 1989


In article <4700036 at m.cs.uiuc.edu> kenny at m.cs.uiuc.edu writes:
++ 
++ Some time ago, I saw in this newsgroup a discussion for a proposal to
++ implement conformant arrays of more than one dimension in C.  If I
++ remember correctly, the proposed scheme was to allow a function to be
++ expressed as:
++ 
++ 	return_type f (int m,		/* Number of rows */
++ 	               int n,		/* Number of columns */
++ 	               double x [] [n]);/* x is a m x n array */
++ 	{
++ 	    .... code for f ....
++ 	}
++ 
++ The generated code would use standard rules for pointer arithmetic,
++ except that the variable n would enter into the computation:
++ 
++ 	sizeof (x [0] [0]) == sizeof (double)
++ 	sizeof (x [0]) == n * sizeof (double)
++ 	sizeof x == sizeof (double (*)[n]) [ == sizeof (double *) ?? ]
++ 
++ and when these rules are applied, we get C's usual row-major storage
++ layout.
++ 
++ Has anyone tried implementing such a scheme?  (I recall that X3J11

Check out GNU GCC 1.35.  I believe it implements what you are
referring to:

----------------------------------------
#include <stdio.h>
/* Try using other types for TYPE, i.e., double, int, float, char, etc. */
typedef long TYPE;

void
tester (int i, int j, TYPE buf[i][j])
{
  int k, l;

  for (k = 0; k < i; k++)
    {
      for (l = 0; l < j; l++)
        printf ("buf[%d][%d] = %4d ", k, l, buf[k][l]);
      putchar ('\n');
    }
}

int
main (int argc, char *argv[]) 
{
  if (argc == 3)
    {
      int i = atoi (argv[1]);
      int j = atoi (argv[2]);
      int k, l;
      TYPE c[i][j];

      for (k = 0; k < i; k++)
        for (l = 0; l < j; l++)
          c[k][l] = k * l;

      tester (i, j, c);
      return 0;
    }
  else
    fprintf (stderr, "usage: %s <row integer> <column integer>\n", argv[0]);
  return 1;
}
----------------------------------------

This seems pretty straight-forward to understand and implement, but
might it present problems on some architectures?

Doug
--
On a clear day, under blue skies, there is no need to seek.
And asking about Buddha                +------------------------+
Is like proclaiming innocence,         | schmidt at ics.uci.edu    |
With loot in your pocket.              | office: (714) 856-4043 |



More information about the Comp.lang.c mailing list