3d array
#7

Quote:
Originally Posted by IstuntmanI
Посмотреть сообщение
I still think that way is a bad. Up-Down should be the first dimension and from the left to the right it should be the second dimension. The compiler wouldn't give any warning or error when you make a mistake.
pawn Код:
new g_Convertibles[][][] =
{
//#           0                1 (second dimension)
//1st dim
/*0*/    {"Stallion",        65000},
/*1*/    {"Comet",           150000},
/*2*/    {"Feltzer",         75000},
/*3*/    {"Windsor",         65000},
/*4*/    {"Stallion",        65000}
};
Good examples for your method:
pawn Код:
printf( "'%c'/%s/%d", g_Convertibles[ 0 ][ 0 ] ); // "'S'/Stallion/83" - ok
printf( "'%c'/%s/%d", g_Convertibles[ 0 ][ 0 ][ 0 ] ); // "'S'/Stallion/83" - ok
printf( "'%c'/%s/%d", g_Convertibles[ 0 ][ 0 ][ 1 ] ); // "'t'/tallion/116" - ok
printf( "%d", g_Convertibles[ 0 ][ 1 ] ); // "65000" - ok
printf( "%d", g_Convertibles[ 0 ][ 1 ][ 0 ] ); // "65000" - ok - but bad, you shouldn't be able to access any additional dimension of an integer, as it doesn't exists
Bad examples, the compiler gives no warning/error:
pawn Код:
printf( "'%c'/%s/%d", g_Convertibles[ 0 ][ 0 ][ 20 ] ); // "'t'/tzer/116" - not ok, should give a compiling error
printf( "%c", g_Convertibles[ 0 ][ 1 ][ 1 ] ); // 'C' - not ok, this result because an integer has no additional dimension, so it will access the next bytes, which is the 'C' from "Comet", should give a warning/error
----------------



Your method is also bad because the compiler won't know exactly what you are doing. There won't be any error/warning in case you do anything with no logic. Example:
pawn Код:
g_OffRoad[ 0 ][ 500000 ]
Also, as I said before, the first dimension should be the number of rows (and it is) and the second dimension should be the number of human-readable columns (in your case it isn't). You can't access it like this:
pawn Код:
g_OffRoad[ 0 ][ 0 ] // name in index 0 with additional dimension for each character - nope
g_OffRoad[ 0 ][ 1 ] // value in index 1 - nope
----------------



That's because his method internally actually looks like this:
pawn Код:
new g_OffRoad[][] =
{
    {'L', 'a', 'n', 'd', 's', 't', 'a', 'l', 'k', 'e', 'r', '\0', 50000 },
    {'R', 'a', 'n', 'c', 'h', 'e', 'r', '\0', 65000}, // further indexes will go in row 3
    {'S', 'a', 'n', 'd', 'k', 'i', 'n', 'g', '\0', 80000}, // further indexes will go in row 4, etc.
    {'M', 'e', 's', 'a', '\0', 45000},
    {'H', 'u', 'n', 't', 'l', 'e', 'y', '\0', 100000}
};
If you access for example g_OffRoad[0][1] it won't give you the price, but the ASCII value for character 'a', which is 97 (around 100, as you said).

Well, the value for the first row (0 - "Landstalker") is placed in g_OffRoad[0][12], as that value of 50000 is in the 13rd index. For the second row it isn't in 13, but in 9th (g_OffRoad[0][8]). If you access the 10th index of row 0 you will get the 'R' from the first index of the second row, which isn't really alright.

----------------

Sorry if I messed something up, I took both OffRoad and Convertibles arrays for different examples, also I wrote-edited, wrote-edited, wrote-edited.

With more advanced things you risk to mess it up and access invalid memory addresses, which can corrupt the memory (better be safe than sorry). I think that my method is the safest one and easy to understand, which gives exact dimensions of everything. It gives enough warnings/errors if you access invalid indexes. You know exactly where is the name and where is the value.
Alright, I will use your method. Thank you.
Reply


Messages In This Thread
3d array - by GoldenLion - 03.01.2017, 22:55
Re: 3d array - by IstuntmanI - 03.01.2017, 23:07
Re: 3d array - by GoldenLion - 03.01.2017, 23:37
Re: 3d array - by SickAttack - 04.01.2017, 00:01
Re: 3d array - by GoldenLion - 04.01.2017, 00:47
Re: 3d array - by IstuntmanI - 04.01.2017, 01:23
Re: 3d array - by GoldenLion - 04.01.2017, 01:52
Re: 3d array - by SickAttack - 04.01.2017, 15:45
Re: 3d array - by GoldenLion - 04.01.2017, 16:21

Forum Jump:


Users browsing this thread: 2 Guest(s)