Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!usc!snorkelwacker.mit.edu!shelby!eos!data.nas.nasa.gov!mustang!nntp-server.caltech.edu!suncub.bbso.caltech.edu!EYCHANER From: eychaner@suncub.bbso.caltech.edu (Amateurgrammer) Newsgroups: comp.lang.c Subject: Re: More 2D array woes... Message-ID: <1991Mar4.030849.16054@nntp-server.caltech.edu> Date: 4 Mar 91 03:08:49 GMT References: <1991Mar3.235114.12154@nntp-server.caltech.edu>,<1002@caslon.cs.arizona.edu> Sender: news@nntp-server.caltech.edu Reply-To: eychaner@suncub.bbso.caltech.edu Organization: Big Bear Solar Observatory, Caltech Lines: 41 dave@cs.arizona.edu (Dave P. Schaumann) writes: >eychaner@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@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@suncub.bbso.caltech.edu "You Have the Right to Remain DEAD." -The Simpsons