Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > variable length arrays

Reply
Thread Tools

variable length arrays

 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      09-26-2003
> 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[i][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[i][j][k] = 100*i + 10*j + k;
printf("A[%d][%d][%d] = %3d\n", i, j, k, a[i][j][k]);
}
}
}
putchar('\n');
Print3DArray(3, 2, a, 4);
return 0;
}

> gcc -Wall -std=c99 -pedantic -O2 -o vararray vararray.c


 
Reply With Quote
 
 
 
 
Aggro
Guest
Posts: n/a
 
      09-27-2003
E. Robert Tisdale wrote:
> > cat vararray.c


Did you ask something?

 
Reply With Quote
 
 
 
 
Kevin Bracey
Guest
Posts: n/a
 
      09-29-2003
In message <(E-Mail Removed)>
"E. Robert Tisdale" <(E-Mail Removed)> wrote:

> > cat vararray.c


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.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      09-29-2003
Kevin Bracey wrote:

> E. Robert Tisdale wrote:
>
>> > cat vararray.c

>
> 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*.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-30-2003
E. Robert Tisdale wrote:

> 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.

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      09-30-2003
Richard Heathfield wrote:

> E. Robert Tisdale wrote:
>
>>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.


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.



 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-30-2003
E. Robert Tisdale wrote:

> Richard Heathfield wrote:
>
>> E. Robert Tisdale wrote:
>>
>>>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.

>
> Yes.


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.

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      09-30-2003
On Tue, 30 Sep 2003 20:19:54 +0000 (UTC), in comp.lang.c , Richard
Heathfield <(E-Mail Removed)> wrote:

>E. Robert Tisdale wrote:
>
>> 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.


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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      10-01-2003
Richard Heathfield wrote:

> E. Robert Tisdale wrote:
>
>>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.


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.

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      10-01-2003
Mark McIntyre wrote:

> Richard wrote:
>
>>E. Robert Tisdale wrote:
>>
>>> 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.

>
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable-length arrays: should they be used at all? Rui Maciel C Programming 56 07-10-2012 06:12 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Variable length arrays Erwin Lindemann C Programming 45 03-08-2008 04:44 AM
Initialization of variable length arrays jaime C Programming 3 06-14-2007 11:56 PM
Variable length arrays Q mechanicfem@googlemail.com C Programming 6 02-21-2006 04:32 AM



Advertisments