variable length arrays

E

E. Robert Tisdale

cat vararray.c
#include <stdio.h>

int Print3DArray(int n3, int n2, int a[][n2][n3], int n1) {
int i = 0;
for(i = 0; i < n1; ++i) {
int j = 0;
for(j = 0; j < n2; ++j) {
int k = 0;
for(k = 0; k < n3; ++k) {
printf("a[%d][%d][%d] = %3d\n", i, j, k, a[j][k]);
}
}
}
return 0;
}

int main(int argc, char* argv[]) {
int a[4][2][3];
int i = 0;
for(i = 0; i < 4; ++i) {
int j = 0;
for(j = 0; j < 2; ++j) {
int k = 0;
for(k = 0; k < 3; ++k) {
a[j][k] = 100*i + 10*j + k;
printf("A[%d][%d][%d] = %3d\n", i, j, k, a[j][k]);
}
}
}
putchar('\n');
Print3DArray(3, 2, a, 4);
return 0;
}
 
K

Kevin Bracey

In message <[email protected]>

Super. It compiled without errors or warnings here, on my (non-gcc) C99
compiler, and printed the array contents twice.

Can't see anything wrong with the code; if you've got a problem it's
probably the compiler.
 
E

E. Robert Tisdale

Kevin said:
Super. It compiled without errors or warnings here,
on my (non-gcc) C99 compiler, and printed the array contents twice.

Can't see anything wrong with the code;
if you've got a problem it's probably the compiler.

It appears to me that
the new variable-length array features in the current standard
reduce much of what appears in the C FAQ to *bad advice*.
 
R

Richard Heathfield

E. Robert Tisdale said:
It appears to me that
the new variable-length array features in the current standard
reduce much of what appears in the C FAQ to *bad advice*.

Only for those with C99 implementations who have no need for their code to
remain portable to C90 implementations.
 
E

E. Robert Tisdale

Richard said:
Only for those with C99 implementations who have no need
for their code to remain portable to C90 implementations.

Yes. That's an accurate description of new users
who are so often admonished to "read the FAQ".
I don't see any reason for these people to learn bad habits
which will only be difficult to break when, eventually,
everyone has new C99 compilers.
The advice in the C FAQ regarding variable length arrays
should be revised. The current advice should be labeled
anachronistic and should be deprecated.
 
R

Richard Heathfield

E. Robert Tisdale said:

i.e. almost nobody.
That's an accurate description of new users
who are so often admonished to "read the FAQ".

No, it isn't. Most new users are still using C90 implementations. Some are
even using /pre/-C90 implementations.
I don't see any reason for these people to learn bad habits
which will only be difficult to break when, eventually,
everyone has new C99 compilers.

The habits are not bad. They are best practice at present. I believe that
Steve Summit once mentioned spending quite a lot of time preparing C99
updates to the FAQ. No doubt he'll release those updates in due course.
The advice in the C FAQ regarding variable length arrays
should be revised. The current advice should be labeled
anachronistic and should be deprecated.

It's currently relevant to the vast majority of C programmers. Most people
don't yet have C99 compilers.
 
M

Mark McIntyre

It's currently relevant to the vast majority of C programmers. Most people
don't yet have C99 compilers.

And even those of us who do, should consider portability issues for
the next decade or so. I still occasionally have to recompile on an
old sun box which has only a pre-ANSI compiler available, and the
majority of my programming is done with a mainstream compiler whose
manufacturer has apparently publically stated they've no immediate
intention of supporting more of C99 than they already (badly) do.
 
E

E. Robert Tisdale

Richard said:
It's currently relevant to the vast majority of C programmers.

But the vast majority of C programmers don't read the FAQ.
Most of the contributors to the comp.lang.c newsgroup
don't read the FAQ. If they did, they could cite and quote the FAQ
instead of answering Frequently Answered Questions.
Most people don't yet have C99 compilers.

They should get C99 compilers and start using them.
 
E

E. Robert Tisdale

Mark said:
And even those of us who do,
should consider portability issues for the next decade or so.
I still occasionally have to recompile on an old sun box
which has only a pre-ANSI compiler available, and the majority
of my programming is done with a mainstream compiler whose
manufacturer has apparently publicly stated they've no immediate
intention of supporting more of C99 than they already (badly) do.

Now you're beginning to sound like a Fortran programmer.

You don't need to learn anything new. Your career will end
long before the last of the C89 compilers are phased out.
But what about the new programmers that are coming up
to replace you? They will all have C99 compilers at their disposal
long before their careers really take off. The only reason
that they might need to learn about these anachronisms
is so that they can read, understand and maintain
your legacy code after you have retired.
Fortunately, you won't be around to hear the "blessings"
that they might utter regarding your code.
 
M

Mike Wahler

E. Robert Tisdale said:
Now you're beginning to sound like a Fortran programmer.

You don't need to learn anything new. Your career will end
long before the last of the C89 compilers are phased out.
But what about the new programmers that are coming up
to replace you? They will all have C99 compilers at their disposal
long before their careers really take off. The only reason
that they might need to learn about these anachronisms
is so that they can read, understand and maintain
your legacy code after you have retired.

In my experience, this is a large part of what the
typical programmer does.
Fortunately, you won't be around to hear the "blessings"
that they might utter regarding your code.

It's illogical to complain about code people wrote
without using tools which were not available at the time.

-Mike
 
M

Mike Wahler

E. Robert Tisdale said:
But the vast majority of C programmers don't read the FAQ.

So? That's their problem, not ours.
Most of the contributors to the comp.lang.c newsgroup
don't read the FAQ.

You have no way of knowing this. You're speculating.
If they did, they could cite and quote the FAQ
instead of answering Frequently Answered Questions.

Extremely poor deduction. You're presuming to read
minds. Smoking is bad for the health. Virtually
everyone knows this. "If they know smoking is bad,
they'd quit." But many don't, even though they know
the facts.

I've read through the entire FAQ a few times,
sometimes I refer folks to it, sometimes I answer the
question. Of course I haven't committed the entire
FAQ to memory, and I don't always check it before answering
a question. Perhaps I should, but I don't, for whatever
reason -- one day perhaps 'laziness', others perhaps
'forgetfulness'. If I do answer, often it's because
it's faster than first scanning the FAQ for the answer.
The fact that I share my time here doesn't mean my
time isn't still valuable to me. Sometimes I'll present
an answer, and then mention that the answer's in the
FAQ too. Sometimes I'll apply what I've learned from
the FAQ in answering a question, then someone will
show that my answer is wrong, causing me to realize
I've misunderstood, and learn something myself.
They should get C99 compilers and start using them.

This is like saying:
You should sell your car every year, and buy the latest
model, even if the new features are not desirable or
useful to you.

I don't have a DVD unit for my TV, I have a VHS unit.
It serves my purposes. Why should I buy a DVD?
I need not until the VHS is insufficient (e.g. when
VHS tapes no longer available, or I want or need
what a DVD can do but a VHS cannot.)

-Mike
 
E

E. Robert Tisdale

Mike said:
So? That's their problem, not ours.


You have no way of knowing this. You're speculating.



Extremely poor deduction. You're presuming to read minds.
Smoking is bad for the health.
Virtually everyone knows this.
"If they know smoking is bad, they'd quit."
But many don't, even though they know the facts.

I've read through the entire FAQ a few times,
sometimes I refer folks to it, sometimes I answer the
question. Of course I haven't committed the entire
FAQ to memory, and I don't always check it before answering
a question. Perhaps I should, but I don't, for whatever
reason -- one day perhaps 'laziness', others perhaps
'forgetfulness'. If I do answer, often it's because
it's faster than first scanning the FAQ for the answer.
The fact that I share my time here doesn't mean my
time isn't still valuable to me. Sometimes I'll present
an answer, and then mention that the answer's in the
FAQ too. Sometimes I'll apply what I've learned from
the FAQ in answering a question, then someone will
show that my answer is wrong, causing me to realize
I've misunderstood, and learn something myself.


This is like saying:
You should sell your car every year,
and buy the latest model,
even if the new features are not desirable
or useful to you.

I don't have a DVD unit for my TV, I have a VHS unit.
It serves my purposes. Why should I buy a DVD?
I need not until the VHS is insufficient
(e.g. when VHS tapes no longer available,
or I want or need what a DVD can do but a VHS cannot.)

I don't want you to give up your VHS, BETA-MAX,
phonograph record player, bell-bottom pants, paisley shirts,
platform shoes, side burns, Volkswagen Beetle, bean-bag chair
or anything else that you have grown fond of over the years.
I just don't think that the "new generation" should be compelled
to make these things a part of their lives as well.
I don't think that new C programmers should be compelled
to adopt anachronisms just because you continue to use them
in the code that you write.
 
M

Mike Wahler

E. Robert Tisdale said:
I don't want you to give up your VHS, BETA-MAX,
phonograph record player, bell-bottom pants, paisley shirts,
platform shoes, side burns, Volkswagen Beetle, bean-bag chair
or anything else that you have grown fond of over the years.
I just don't think that the "new generation" should be compelled
to make these things a part of their lives as well.

IMO nor should they be discouraged from doing so if they
so choose. I said nothing about 'compelling'.
Of course it's a good idea to educate them
about the more 'modern' alternatives, but the choice
is still theirs, the consequences of which they of
course must be prepared to face.
I don't think that new C programmers should be compelled
to adopt anachronisms just because you continue to use them
in the code that you write.

I'd hardly call C90 an 'anachronism'. It's still the
most widely used form of C, AFAIK.

-Mike
 
M

Mark McIntyre

You have no way of knowing this. You're speculating.

Not only that, but empirical evidence suggests otherwise. For example
100% of the CLC contributors that I know personally have read the FAQ.
I've read through the entire FAQ a few times,
sometimes I refer folks to it, sometimes I answer the
question. Of course I haven't committed the entire
FAQ to memory, and I don't always check it before answering
a question. Perhaps I should, but I don't, for whatever
reason

Typically I don't check it because thats what the OP is supposed to
do, dammit ! What, I have to do his homework /and/ read the FAQ for
him? Why not just get his degree, marry his wife, and collect his
paycheck too ?
 
E

E. Robert Tisdale

Mark said:
Typically,
I don't check it because that's what the OP is supposed to do, dammit!
What, I have to do his homework /and/ read the FAQ for him?

No.
But you could read the FAQ once in a while yourself.
You know -- set a good example and all that.
Why not just get his degree, marry his wife
and collect his paycheck too?

So much ambition for someone who won't even read the FAQ?

I just don't think that it makes any sense
to admonish new subscribers to "read the FAQ"
if you refuse to do so every once in a while yourself.
And, if you had read the FAQ, it doesn't seem unreasonable
that you could quickly find, cite and even quote
the relevant FAQ for the OP.
 
J

Joona I Palaste

Mark McIntyre said:
Typically I don't check it because thats what the OP is supposed to
do, dammit ! What, I have to do his homework /and/ read the FAQ for
him? Why not just get his degree, marry his wife, and collect his
paycheck too ?

That would suit me...
(AFAICR you Mark are already married, so you don't need a wife. Give
that one to me... =) )

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"C++ looks like line noise."
- Fred L. Baube III
 
M

Mark McIntyre

That would suit me...
(AFAICR you Mark are already married, so you don't need a wife. Give
that one to me... =) )

Depending on the size of the paycheck, I'm prepared to move to Utah...
:)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top