4 ms·
I underline what you said and add to it: I started with arrays with Fortran. There can pass an array as an argument to a parameter of a subroutine, and the su
by graycat 10y ago
I underline what you said and add to it:
I started with arrays with Fortran. There can pass an array as an argument to a parameter of a subroutine, and the subroutine can address the darned array
any way it wants. Why? Sure, maybe want to sort the array based on a slice here omitting details. Sure, such programming would need a lot of well done documentation and still is dangerous. And such array fooling around stands to get into trouble with compiler options to check array bounds.
Then I did a lot with PL/I: There get arrays, structures, structures of arrays of structures of arrays, etc. Also get to set the lower and upper bounds of each array index to anything -- positive, negative, or zero, including during execution -- that can be represented in the usually 16 bit array indexing arithmetic. And, it turns out, all that array indexing, even for arrays of structures of arrays, etc., is just ordinary multi-dimensional array arithmetic -- nearly as useful as objects but with much simpler storage allocation and array indexing. And if look carefully at that arithmetic, then do see how to have any subscript (index) lower bounds and upper bounds with just trivial modification of the usual array indexing.
And, yes, when I wrote C code, no way did I like the high emphasis on starting an array at index 0. Besides C doesn't even really support arrays, e.g., with several subscripts, and forces a programmer to write a macro or some such that does the arithmetic. And there are problems in a subroutine that receives such an array. And compiler support for checking array bounds is in trouble because the language syntax and semantics don't even support arrays with several dimensions. So, have to write, say, a matrix package that has a C structure for the details of number of dimensions and the upper/lower bounds for each dimension -- it works but get no compiler based debugging support or I/O support, and no doubt the compiler can't do some indexing optimizations that Fortran and PL/I can do because the data being used for the indexing is, in the semantics, not subject to being changed by the user's code during the indexing arithmetic. E.g., with PL/I, in the execution logic, each structure or array, each aggregate has a little table, a dope vector that describes the aggregate, yes, within a subroutine and within runtime subroutines, e.g., for doing I/O on the whole aggregate.
PL/I was in good shape before C was developed. PL/I was used for the operating systems Multics and much of Primos. So, PL/I offers programmers much more than C and, still, is good for both applications and operating system code, especially in array indexing.
Sorry, Dijkstra.
- drivers99 10y agoC supports multidimensional arrays. https://learnxinyminutes.com/docs/c/ https://learnxinyminutes.com/docs/c/ then text seach for "dimensional". More detail: http://beginnersbook.com/2014/01/2d-arrays-in-c-example/ http://beginnersbook.com/2014/01/2d-arrays-in-c-example/
- graycat 10y agoThanks. I didn't see that in K&R, 1988. Are such multi-dimensional arrays new since 1988? When I needed to do a little matrix work in C, I just wrote my own little package of matrix code, maybe with more functionality than I just saw in your first link. Maybe I didn't really need to write my own?
- drivers99 10y agoI'm not sure. I only started learning C around 1991. I could see how you might still have to work around using the arrays. "a[4][4]" as an example allocates memory contiguously, but if you pass a pointer to a, you lose the dimensionality (it's more like "a[16]"). So you'd have to have some other way to pass the dimensions in and you'd have to calculate the location of elements yourself. Otherwise, you'd have to pass it by value (if the dimensions are known at compile time and don't change) which can be very inefficient (copying large blocks of memory). I thought maybe you were getting at that when you said C doesn't "really" support it. The more I look into how to pass a multi-dimensional array around in C, the more I think you are more right than I was. It doesn't really have it, it's more like a "recursive use of the type system" as one link was explaining it.