473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

3d array

hi all, i need to knw abt 3d arrays, is it compulsary to specify all the dimensions in 3d array, ex: char [] [] [], whar r the mandataory fields, can we give variable length dimensions for length , width n depth....
Sep 3 '07 #1
1 1748
weaknessforcats
9,208 Expert Mod 8TB
Firstly, there are no 3D arrays in C++. There are only one-dimensional arrays. You use the [] operator to define the number of elements:

Expand|Select|Wrap|Line Numbers
  1. int array[3];
  2.  
This is an array of 3 elements. Each element is an int.

Expand|Select|Wrap|Line Numbers
  1. int array[3][4];
  2.  
This is an array of 3 elements. Each element is an array of 4 int. Now some folks may call this a 2D array but it really is a one-dimensional array where the elements are arrays.

Therefore:
Expand|Select|Wrap|Line Numbers
  1. int  array[3][4][5];
  2.  
is an array of 3 elements. Each element is and array of 4 elements each of which is an array of 5 ints.

From this you can deduce the following rules:
1) For a stack array you must specify the number of elements at compile time.
2) For a dynamic array you may specify the number of elements at run time.
3) The name of the array is the address of element 0.

Expand|Select|Wrap|Line Numbers
  1. int array[3];
  2. int* ptr = array; //OK. Element 0 is an int. Hence, "array" is the address of an int
  3. int array1[x];    //ERROR. Number o elements no specified.
  4.  
Expand|Select|Wrap|Line Numbers
  1. int array[3][4]; 
  2. int (*ptr)[4] = array; //OK. Element 0 is an array of 4 int. Hence "array" is the address of an array of 4 int.
  3. int** ptr1 = array;  //ERROR. Element 0 is not the address of hte address of a single int.
  4.  
Expand|Select|Wrap|Line Numbers
  1. int array[3][4][5]; 
  2. int (*ptr)[4][5] = array; //OK. Element 0 is an array of 4 arrays of 5 ints. Hence "array" is the address of an array of 4 elements each of which are arrays of 5 int.
  3. int*** ptr1 = array;  //ERROR. Element 0 is not the address of ther address of the address of a single int.
  4.  
Dynamic allcoations require yout specify the number of elements at run time.
Remember, the number of elments is the first index:
Expand|Select|Wrap|Line Numbers
  1. int num;
  2. cin >> num;
  3. int* array = new int[num];           //OK
  4. int (*ptr)[4] = new int[num][4];     //OK
  5. int (*ptr)[4][5]  = new int[num][4][5];  //OK
  6.  
Sep 3 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.