Malloc and arrays...

Amateurgrammer eychaner at suncub.bbso.caltech.edu
Mon Mar 4 03:29:42 AEST 1991


Warm up those flame guns!  It's time for Another Novice Question!

I know this is partly covered in the FAQ, but it didn't seem to have quite
the answer I want.
I know that if I malloc a chunk of memory, like so:
  char *bigmemory;
  bigmemory = (char *) malloc (OODLES_O_BYTES);
I can access the chunk of memory as though it were an array, like so:
  bigmemory [RIGHT_HERE] = SOME_CHARACTER;
(I hope I have this right. This should decompose to:
  *(bigmemory + RIGHT_HERE) = SOME_CHARACTER;
shouldn't it?)
So how can I access it as a 2D array, like so:
  bigmemory [RIGHT_HERE][RIGHT_NOW] = SOME_CHARACTER;
I know I could use, for example:
  *(bigmemory + RIGHT_HERE * SIZEOF_RIGHTNOW + RIGHT_NOW) = SOME_CHARACTER;
but all my previous code (which uses the 2D array) would then have to be
changed.  And it looks much nicer (when RIGHT_HERE and RIGHT_NOW are big,
nasty expressions) in 2D array format.
And don't flame me for "writing bad code"; it was written during a kinder,
gentler period.  I'm doing the best that I can, learning as I go along.
								-Glenn
:-)
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