More 2D array woes...

Amateurgrammer eychaner at suncub.bbso.caltech.edu
Mon Mar 4 13:08:49 AEST 1991


dave at cs.arizona.edu (Dave P. Schaumann) writes:
>eychaner at suncub.bbso.caltech.edu writes:
>>Here we go again, warm up the flame guns...
>>
>>Ok, suppose I have an array like
>>    char strings[NUMBER][SIZE];
>>How do I add more strings to this array, i.e. make it larger, portably and
>>easily.  I can't realloc it, since it's not a pointer!  Argh!
>
>It's beginning to sound like what you really want is C++.  In C++, you can
>simulate arrays cleanly using pointers, and thus you can use realloc to
>increase (or decrease) the size of your array dynamically.  You could even
>program it so that accessing an element beyond the end of your array
>automagically made it bigger.

Well, actually, Chris Torek (torek at ee.lbl.gov) pointed out that the "cheap
and dirty way" (my phrase, not his) of doing this is to declare
      char (*dynamic)[N];
      dynamic = (char (*)[N]) malloc (M * N);
where N is the number of items in each row of the array, and M is the
number of rows.  This array can then be accessed by
      dynamic[x][y] = SOME_CHAR;
and reallocated by
      dynamic = realloc (dynamic, NEWSIZE * N);
so you CAN do it in C.  It's just a little tricky.  Ok, a LOT tricky.

>C is good for writing quick, reasonably simple (at least as far as data
>structure manipulation goes) programs.  When you find you need some
>heavy-duty data structures, like dynamically sized arrays, or associative
>arrays, you should seriously consider moving to C++.  The learning curve
>is kind of steep (even for C-literates), but ultimately a useful tool to
>be able to use.

Yeah.  So point out some C++ compilers for VMS that are reasonably priced,
and my boss will be more than happy to turn down my request for one.  No
smiley here, it's true.  I'm stuck with C, and that's how it is.  Unless I
want to use FORTRAN.  Ugh.
:-)
Glenn Eychaner - Big Bear Solar Observatory - eychaner at suncub.bbso.caltech.edu
"You Have the Right to Remain DEAD."
					-The Simpsons



More information about the Comp.lang.c mailing list