delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/02/18/17:15:04

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
Sender: mike AT zeus DOT local
Newsgroups: comp.os.msdos.djgpp
Subject: Re: re array of arrays
References: <hnVb8.41607$JZ DOT 3853549 AT news20 DOT bellglobal DOT com>
From: Michael Farnham <mcfarnham AT prodigy DOT net>
Message-ID: <m3sn7ysaya.fsf@zeus.local>
Lines: 80
User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Artificial Intelligence)
MIME-Version: 1.0
NNTP-Posting-Host: 208.188.24.47
X-Complaints-To: abuse AT prodigy DOT net
X-Trace: newssvr17.news.prodigy.com 1014069881 ST000 208.188.24.47 (Mon, 18 Feb 2002 17:04:41 EST)
NNTP-Posting-Date: Mon, 18 Feb 2002 17:04:41 EST
Organization: Prodigy Internet http://www.prodigy.com
X-UserInfo1: Q[R_PJSCOHUUBULY\RHT_V AT AO@VDB\XILA]T]_MIJQR AT EPIB_VUKAH_[MTX\IS[K[NGYJJFNOFZR_G[BUNTAOQLFE^TEHRPI]PZZRP_BMDSFQFL_]CBHXRWCMDCUZAZN AT D_AKMNLEI]MWHCSXL^]NNC__CZFGSGHYYXWPFG AT SCAVA]\FT\@B\RDGENSUQS^M
Date: Mon, 18 Feb 2002 22:04:41 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"PM" <sendto DOT paulm AT sympatico DOT ca> writes:

> sorry, I'll clarify the previous post
> 
> I have an array. Lets call this arr. The array has, lets say, 5 elements
> so.. it looks like this
> __________________________________________
> |________|________|________|________|________|
> 
> now... inside EACH of these array elements, I would like to have more
> arrays, lets say 4 elements in size.. so it would look like this
> 
> __________________________________________
> |________|________|________|________|________|
>      ___           ___           ___            ___           ___
>     |___|          |___|         |___|           |___|         |___|
>     |___|          |___|         |___|           |___|         |___|
>     |___|          |___|         |___|           |___|         |___|
>     |___|          |___|         |___|           |___|         |___|
> 
> So now, I would like to access, say, the 3rd element in the array, that is
> inside of the 2nd array (arr). eg.
> 
>  __________________________________________
> |________|___2____|________|________|________|
>      ___           ___           ___            ___           ___
>     |___|          |___|         |___|           |___|         |___|
>     |___|          |___|         |___|           |___|         |___|
>     |___|          |_3_|         |___|           |___|         |___|
>     |___|          |___|         |___|           |___|         |___|
> 
> 
> Now, I would not like to code this, mutlidimensionally. eg arr[5][4]..
> because the rest of my coding wouldn't be compatible..
> 

Technically speaking c and c++ do not have multidimensional arrays. What
they have are arrays of arrays. In other words:

Suppose we have an array named a with 4 elements.
Suppose that each element of a is an array of 4 ints.

a would be declared as follows:

int a[4][4];

The fourth element of the 3rd array contained in a
would be accessed as follows:

a[2][3];

Perhaps what you really want is an array of structures:

struct item
{
  char name[20];
  double count;
  double price;
}

struct item itemArr[10];

Now you would access this array as follows:

strcpy(itemArr[0].name, "potato");   /* Name of item */
itemArr[0].count = 5.0;              /* number of pounds */
itemArr[0].price = 3.59              /* Cost per pound */

/*
 * Note that in a real world program one would represent
 * money using integers to avoid the inherent inaccuracy
 * of all floating types.
*/

HTH
Mike Farnham

-- 
You've got to be honest; if you can fake that, you've got it made.
                         -- George Burns

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019