Velocity Reviews > A[x][y][z]

# A[x][y][z]

Roberto Waltman
Guest
Posts: n/a

 09-30-2005
On 30 Sep 2005 03:28:05 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>what I had in mind and meant with "pre-compiler" or "preprocessor"
>is a small utility which reads the C-"source" containing A[x,y]
>or int A[n] and converts it into another C-source-file
>where A[x,y] is converted into A[x][y] and int A[n]; into
>int A[n+1] and maybe some other things too.
>
>This can't be so difficult to write and probably already
>exists somewhere ?!?

(A) Converting A[x,y] into A[x][y]

That could work only under the assumption that an expression such as
A[x,y] will never occur in you code.

How can a preprocessor know if you mean a two-dimensional array or a
unidimensional one, where the comma operator is used to generate the
index?

A[z]
A[y,z]
A[x,y,z]
A[p,q,r,s,t,u,v,w,x,y,z]

These are all valid C expressions, all of them selecting the 'z'th
element of the unidimensional array A.

(B) Converting int A[n] into A[n+1];

(b.1) While that's more easily done, it may have many undesirable side
effects.
The loop (for i=0; i < MAX; i++) {...A[i]...} is a very well C known
idiom, while (for i=1; i<= MAX; i++) {...A[i]...} is not.
You can expect many "off by one" errors when using this approach with
"C" way.

(b.2) Use C++. Use only the set of features common with C (*), so you
can fool yourself into thinking it is C, but define a set of classes
and overloaded [] operators providing the functionality you want.

(*) Yes, I know the "C is not a proper subset of C++" drill

Roberto Waltman

[ return address is invalid. ]

Keith Thompson
Guest
Posts: n/a

 09-30-2005
(E-Mail Removed) writes:
> what I had in mind and meant with "pre-compiler" or "preprocessor"
> is a small utility which reads the C-"source" containing A[x,y]
> or int A[n] and converts it into another C-source-file
> where A[x,y] is converted into A[x][y] and int A[n]; into
> int A[n+1] and maybe some other things too.
>
> This can't be so difficult to write and probably already
> exists somewhere ?!?

Flash Gordon has already reminded you about the context thing, so I

If you really wanted to, you could do something like this:

double A[10][20][30];
#define A_(x,y,z) (A[(x)][(y)][(z)])

You could then write A_(x,y,z) and have it expand to the equivalent of
A[x][y][z], but with extra parentheses to avoid operator precedence
problems.

A problem with this is that the scope of A is the same as for any
object declared at that point, but the scope of the A_ macro goes from
the point of its definition to the end of the file. Also, since
A_(x,y,z) doesn't use square brackets, it doesn't look like an array
indexing operation. A (possibly inline) function with the same
profile won't work because you can't assign to the result. A function
returning a pointer to the array element would require you to
explicitly dereference the pointer whenever you want to use it.
Personally, I find any of these options uglier than A[x][y][z], but

As for wanting arrays to be 1-based, the book _Numerical Recipes in
C_, available at <http://www.library.cornell.edu/nr/bookcpdf.html>,
uses some tricks that do this. I haven't looked at them in detail,
but I suspect they might invoke undefined behavior by using a base
address before the beginning of the array object (which will happen to
work if that base address happens to be within the address space of
the program).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Mabden
Guest
Posts: n/a

 10-02-2005
"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> (E-Mail Removed) writes:
> > what I had in mind and meant with "pre-compiler" or "preprocessor"
> > is a small utility which reads the C-"source" containing A[x,y]
> > or int A[n] and converts it into another C-source-file
> > where A[x,y] is converted into A[x][y] and int A[n]; into
> > int A[n+1] and maybe some other things too.
> >
> > This can't be so difficult to write and probably already
> > exists somewhere ?!?

Why would it. C programmers write C code.

> If you really wanted to, you could do something like this:
>
> double A[10][20][30];
> #define A_(x,y,z) (A[(x)][(y)][(z)])
>
> You could then write A_(x,y,z) and have it expand to the equivalent of
> A[x][y][z], but with extra parentheses to avoid operator precedence
> problems.
>
> A problem with this is that

.... it's not C and no one else will know what the Hell is going on. So
this person should use a language they like or write their own.

You could make a Pascal programmer happy with a #define that changed {
and } into BEGIN and END, but it's not going to let them write a C
program, which is what they are trying to do.

> As for wanting arrays to be 1-based

.... get the **** over it (the OP, not you Keith). In C the index is an
OFFSET!!!

Get it?!! The first member is OFFSET by NOTHING! It is the first one.
You are already there!! You don't take a step forward. You don't drive a
mile to get there - you are home. You are at Ground Zero! They don't
call it "Ground One"!!!

You can't change it, you can't hide it, we don't want to know why some
other language does it differently...

Deal with it!

--
Mabden

Mabden
Guest
Posts: n/a

 10-02-2005
"Frodo Baggins" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> > Let's combine the goodies from each language !

>
> Reminds me of Perl ; best of sed,awk,sh,etc combined.

C#

--
Mabden

Netocrat
Guest
Posts: n/a

 10-02-2005
Keith Thompson wrote:

> C doesn't actually have multi-dimensional arrays. It only has
> one-dimensional arrays, but the element type can itself be an array
> type; an array of arrays of arrays acts very much like a 3-dimensional
> array.

This is a false distinction, as evidenced by the standard:
6.5.2.1 Array subscripting
"Successive subscript operators designate an element of a
multidimensional array object."

I've pointed this quote out in the past, if I recall correctly you were
even involved in the thread where the subthread of "does C have
multidimensional arrays" sparked up.

> If you want to program in C, you should learn C, with all its little
> quirks. If you dislike C so much, you should pick a different
> language.

Right - in C arrays index from 0 and multi-dimensional arrays are
indexed with successive square bracket enclosures. Anything else is
not C and at best will confuse a C programmer who has to maintain the
code (as Flash Gordon has pointed out the proposed scheme has worse
implications).

sterten@aol.com
Guest
Posts: n/a

 10-02-2005
I won't run that converting utility in the rare cases, when I use
the comma-operator.
And when I show the code to other C-programmers I could still
show them the converted code.

So, who will be the first to write that useful command line utility
converting occurrances of A[x,y] to A[x][y] and int A[n]
to int A[n+1] (or similar)
for the benefit of all C-programmers ?

Keith Thompson
Guest
Posts: n/a

 10-02-2005
"Mabden" <mabden@sbc_global.net> writes:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...

[...]
>> As for wanting arrays to be 1-based

>
> ... get the **** over it (the OP, not you Keith). In C the index is an
> OFFSET!!!
>
> Get it?!! The first member is OFFSET by NOTHING! It is the first one.
> You are already there!! You don't take a step forward. You don't drive a
> mile to get there - you are home. You are at Ground Zero! They don't
> call it "Ground One"!!!
>
> You can't change it, you can't hide it, we don't want to know why some
> other language does it differently...
>
> Deal with it!

And we wonder why people think comp.lang.c is a hostile newsgroup.

Asking how to implement 1-based arrays in C is a perfectly legitimate
question. It happens that the answer is that there's no really good
way to do it, though there are a number of not-so-good ways to do it.

There are applications in which 1-based arrays are more convenient
than 0-based arrays. C's 0-based arrays are suited for systems
programming and for the convenience of the compiler. 1-based arrays
are much more convenient for some mathematical algorithms (see
_Numerical Recipes in C_, which I cited upthread). C's lack of
support for arrays starting at an index other than 0 is a
(justifiable) weakness of the language, not an immutable law of nature
justifying verbal abuse of anyone who questions it.

A future C standard, or an upward-compatible extension, could allow
an array to be defined either as
double arr[LENGTH];
or as
double arr[LO..HI];
Existing code would not be affected (unless I'm missing something).
Code that uses the new form would incur the overhead of computing the
offset rather than assuming it's 0 (the indexing operator would be
defined differently for based arrays. I'm not sure whether this would
be worth doing; it's always tempting to add just one more feature, but
the end result of too many such additions would be a *much* more
complex language.

<OT>
to do. The OP might want to look into it, but we don't discuss C++
here. Try comp.lang.c++.
</OT>

There may well be some stupid questions, but there aren't many.
Stupid answers are far more common.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Flash Gordon
Guest
Posts: n/a

 10-02-2005
(E-Mail Removed) wrote:

<snip>

> So, who will be the first to write that useful command line utility
> converting occurrances of A[x,y] to A[x][y] and int A[n]
> to int A[n+1] (or similar)
> for the benefit of all C-programmers ?

How can you have failed to notice that NONE of the C programmers here
think it is a good idea or would be of benefit to anyone? The reasons
have been explained multiple times in different ways.

Since you seem to have an an inability to grasp the simple concepts
involved and won't say why you consider any of the stated reasons to be
invalid I can only conclude that you are a troll.

*plonk*
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.

Keith Thompson
Guest
Posts: n/a

 10-02-2005
"Netocrat" <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> C doesn't actually have multi-dimensional arrays. It only has
>> one-dimensional arrays, but the element type can itself be an array
>> type; an array of arrays of arrays acts very much like a 3-dimensional
>> array.

>
> This is a false distinction, as evidenced by the standard:
> 6.5.2.1 Array subscripting
> "Successive subscript operators designate an element of a
> multidimensional array object."
>
> I've pointed this quote out in the past, if I recall correctly you were
> even involved in the thread where the subthread of "does C have
> multidimensional arrays" sparked up.

Ok, good point (though I don't remember the previous discussion).

I'll rephrase. C has multidimensional arrays. They're implemented
as, and are indistinguishable from, arrays of arrays.

There are other languages that make the distinction. For example,
Pascal has both multidimensional arrays:
arr1: array[1..10,1..10] of float;
and arrays of arrays:
arr2: array[1..10] of array[1..10] of float;
with distinct syntax for indexing into them (arr1[x,y] vs. arr2[x][y])
and different semantics (you can easily extract a one-dimensional
array from an array of arrays, but not from a multidimensional array).

So yes, C has multidimensional arrays in much the same way that it has
pass-by-reference: it lets you build it explicitly on top of other
constructs. I'm not convinced the statement about multidimensional
arrays in 6.5.2.1 is strictly necessary (I think it probably follows
from other statements in the standard), but I have no problem with it
being re-stated.

[snip]

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson
Guest
Posts: n/a

 10-02-2005
(E-Mail Removed) writes:
> I won't run that converting utility in the rare cases, when I use
> the comma-operator.
> And when I show the code to other C-programmers I could still
> show them the converted code.
>
> So, who will be the first to write that useful command line utility
> converting occurrances of A[x,y] to A[x][y] and int A[n]
> to int A[n+1] (or similar)
> for the benefit of all C-programmers ?

Who will be the first to write that useful command line utility
converting followups posted through groups.google.com so they include
proper quoting and attributions, so I can stop posting this:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the

because it's a bad idea; it's because, as far as I can tell, there's
no real demand for such a thing other than you.

If you really want this thing implemented, you'll need at least two
things.

First, you'll need a rigorous specification of what you want,
including how it's going to handle corner cases. You can say that it
will refuse to translate certain things, or that it's undefined
behavior, but you should cover all the cases explicitly. This will
require a fairly thorough knowledge of C. Since you've expressed an
unwillingness to obtain such knowledge, you'll need some help with
this.

Second, you'll need to motivate someone who has sufficient time and
expertise to work on this. Since I doubt that anyone who has the
ability to do this would be interested in working on it for its own
sake (though I could easily be mistaken on that point), the most
effective motivator is likely to be money, probably a lot of it. I
don't know whether you're in a position to provide this motivation.

You'll have to spend some time working with whoever agrees to do this
for you, and you'll have to spend some additional time waiting for it
to be completed. I'm not going to try to estimate how much time.

I suggest that learning to use C as it exists is likely to take less
of your time, and none of anyone else's time, and little or no money.

What you do is, of course, entirely up to you, but that's my advice.

<OT>
You might also consider looking at C++. It implements operator
overloading and has a large runtime library called the STL; it may be
possible to do what you're looking for within the language. If you
have any questions about C++, comp.lang.c++ is down the hall on the
left (though you should consider studying the language on your own
</OT>

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.