Question about array (multidimensional)

Mar 24, 2008 at 2:15pm
Hello! I don't know if this is the correct forum, sorry, i am new here..

Well... i am using this method of array declaration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
char* aFieldMain[]{
"name",
"id",
"password",
"control",
"sysmodel"
};

char* aFieldSub[]{
"tel",
"id",
"address"
};

char* aFieldExtra[]{
"fav",
"access",
"model",
"option",
"controlsys",
"method",
"controlvar",
"limitation",
"register"
};


1)Well... there isn't a way that i can put all these arrays in one array in cpp?

for example, in PHP we would do like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php

$mainarray=array(
array("name","id","password","control","sysmodel"),
array("tel","idz","address"),
array("fav","access","model","option","controlsys","method","controlvar","limitation","register")
);
//and by showing the value content like this:

printf($mainarray[1][2]);
echo "\n<br>\n";//jump a html line, or the output will be in one line
//output:"idz"


printf($mainarray[0][4]);
echo "\n<br>\n";
//output:"sysmodel"


printf($mainarray[2][8]);
echo "\n<br>\n";
//output:"register"
?>



by the way... remember that we may not know the number of elements in each array.

And too, i already visited this section : http://www.cplusplus.com/doc/tutorial/arrays.html#multidimensional_arrays
but they have defined arrays. i don't want to declare the number of elements in each array.


that ends my question.

So.... the following ones are just a curiosity, i know they hardly will work in cpp.

2)in php, we can do that:
1
2
3
4
$array1=array(
array("test1","test2"),
"abcd" => array(1,2)
);

This is possible in cpp?


We can go furthermore:

3)
1
2
3
4
5
$array1=array(
2000   => array("test1","test2"),
"abcd" => array(array("a1",1),array("2",true))
);


I really don't think that is possible. by the way...... on examples 2 and 3, please forget the
vartype on cpp. just think that they are all char.

Thank you for your patience.
Last edited on Mar 24, 2008 at 2:46pm
Mar 24, 2008 at 4:01pm
1) You can declare multi dimentional arrays like this, but all "internal" arrays must have the same length:
1
2
3
4
5
char* aField[][4] = {
  {"foo", "bar", "foo", "bar"},
  {"foobar", "foobaz", "foobar", "foobaz"}
  // ...
};


You can use an array of pointers to get almost the same effect as an array of arrays of different size:
1
2
3
4
// Declare aFieldMain, aFieldSub, aFieldExtra
char **array[] = { aFieldMain, aFieldSub, aFieldExtra };
// array[0][0] == "name"
// array[1][1] == "id" 


2,3)
I don't know what that means, but I guess it's something like a std::map. Only in C++ you are required to have the same datatype all keys and all values.

http://cplusplus.com/reference/stl/map/
Mar 24, 2008 at 5:13pm
Hi!

Hm.... okay then.... i think i'll continue using the old one..

about the 2 and 3..... well. imagine a array of 5 dimensions, etc.... but i think i've understood...

Thank you very much.
Last edited on Mar 24, 2008 at 5:15pm
Topic archived. No new replies allowed.