Mail Archives: djgpp/2002/02/18/17:15:04
"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 -