2D arrays and pointers in ANSI C. (source)

Karl Heuer karl at ima.isc.com
Wed Dec 5 07:19:44 AEST 1990


In article <1990Dec2.125713.19371 at batcomputer.tn.cornell.edu> christos at batcomputer.tn.cornell.edu (Christos S. Zoulas) writes:
>I think the following piece of code will solve your problem:
>[code to implement a function that purports to allow allocations like:]
>	ptr = (thing **)arrayn(2, M, N, sizeof(thing));
>	... ptr[i][j] ...

Although this probably works fine on vaxlike architectures, it will fail on
machines where `void *' and `thing *' have different internal representations
(such as DG).  This cannot be fixed without help from a compiler builtin, and
hence any program that uses arrayn() is unportable.

The only way this could become portable would be if the Standard were to
mandate its existence% (and hence require nonvaxlike implementations to supply
a simple crutch as a compiler builtin).  The Committee probably won't do so
unless there's existing practice, and they probably won't accept existing
practice because it's unportable code.

(Nice job, though.  I never bothered with more than 2D in my implementation,
before I threw it out for the reason described above.)

Karl W. Z. Heuer (karl at ima.isc.com or uunet!ima!karl), The Walking Lint
________
% The "standardizable" version would have to be a macro which expects one
  parameter to be a type name (e.g. array2(thing, M, N)), since "sizeof" might
  throw out too much information.  This is a further complication for the
  generic version, since C does not support variadic macros.



More information about the Comp.lang.c mailing list