![]() |
| ||
| (reformatted) How to return Multi-Dimensional Arrays Hello: (I am using Borland C++ Builder 6 Professional) Before you jump all over me I've already read the post Returing Arrays C/C++ and completely understand. For some reason I am still having difficulty implementing in my project. Very brief Project Explanation: I am collecting data from a laser sensor and want to display my data in cartesian coordinates. I've already created a class that has captured the raw data, converted it to cartesian coordinates and stored these coordinates in an array. Return the array to my main form is what I am having trouble with. Here is a snippet of the member functions I am referring to (sorry about the long code most of which is of no interest to you) data gets stored in array xyz_data [PHP]void scan_data::add_image(VARIANT & data_array) { if (current_scan>=10) { current_scan=0; } // Check if the variant contains a SAFEARRAY // Check if the SAFEARRAY is 1D if (SafeArrayGetDim (V_ARRAY (&data_array)) != 1 ) { // Not a 1D SAFEARRAY throw ("Logic error css_image::add_image()"); } SAFEARRAY * pArray = V_ARRAY (&data_array); long * prgn; long elements; long i, peaks; // Get bounds of array and a pointer to its data if ((FAILED (SafeArrayGetLBound (pArray, 1, &i))) || (FAILED (SafeArrayGetUBound (pArray, 1, &elements)))|| (FAILED (SafeArrayAccessData (pArray, (void **)&prgn)))) { // Failed to get bounds or pointer throw ("HRESULT error css_image::add_image()"); } // Create a new image elements += 1-i; long * end = prgn + elements; int j=0; unsigned int pos, str; scan_points[current_scan]=0; long ii; while (prgn != end) { scan_points[current_scan]+=1; long state=(*prgn)&0xFFFF; ii=(*prgn&0xFFFF0000)>>16; ++prgn; if (ii > 0) //check if any peaks exist, if they do, get position and strength { //store the first peak in the raw_data array at the position of the next available scan pos=*prgn; raw_pos[current_scan][j]=pos; str=*(prgn+1); raw_str[current_scan][j]=str; prgn+=2; } switch(state) { case 0,void_sample: status[current_scan][j]=dropout; break; case saturated_sample: status[current_scan][j]=saturated; break; case good_sample: status[current_scan][j]=normal; break; default: status[current_scan][j]=dropout; } j++; if (j>=Cycle) { break; } --ii; while (ii >0) { //ignore the results of the remaining peaks for this pixel position prgn+=2; --ii; } } SafeArrayUnaccessData (pArray); // Make xyz data if we can if (!pCurrentCSS) { // Can't make xyz data return; } VARIANT xyz_array; if (pCurrentCSS->convert_to_xyz (&data_array, &xyz_array)!=S_OK) { throw ("HRESULT error: css_image::add_image"); }; pArray = V_ARRAY (&xyz_array); xyz_vector * pxyzelem; // Get bounds of array and a pointer to its data if ((FAILED (SafeArrayGetLBound (pArray, 1, &i))) || (FAILED (SafeArrayGetUBound (pArray, 1, &elements)))|| (FAILED (SafeArrayAccessData (pArray, (void **)&pxyzelem)))) { // Failed to get bounds or pointer throw ("HRESULT error css_image::add_image()"); } elements += 1-i; elements /= 3; const double max = 10000.0; for (i = 0; ((i < elements) && (i<Cycle)); ++i) { if (pxyzelem->x < max) { xyz_data[current_scan][0][i] = pxyzelem->x; xyz_data[current_scan][1][i] = pxyzelem->y; xyz_data[current_scan][2][i] = pxyzelem->z; } ++ pxyzelem; } // Destroy the xyz safearray SafeArrayUnaccessData (pArray); SafeArrayDestroy (pArray); current_scan++; }[/PHP] void scan_data::get_image_data(double data[3][Cycle]) The data[][] array is what I need to return to my mainform. This is the original code and not any of my failed attempts. Thanks in advance |
| ||
| Re: (reformatted) How to return Multi-Dimensional Arrays 1 Attachment(s) Array An array is a collection of like objects. The simplest case of an array is a vector. C++ provides a convenient syntax for declaration of fixed-size arrays: decl-specifiers dname [ constant-expressionopt ] ;The number of elements in the array is given by the constant-expression. The first element in the array is the 0th element, and the last element is the (n-1th) element, where n is the size of the array. The constant-expression must be of an integral type and must be greater than 0. A zero-sized array is legal only when the array is the last field in a struct or union and when the Microsoft extensions (/Ze) are enabled. Arrays are derived types and can therefore be constructed from any other derived or fundamental type except functions, references, and void. Arrays constructed from other arrays are multidimensional arrays. These multidimensional arrays are specified by placing multiple [ constant-expression ] specifications in sequence. For example, consider this declaration: int i2[5][7]; It specifies an array of type int, conceptually arranged in a two-dimensional matrix of five rows and seven columns, as shown in Figure 7.2. Figure 7.2 Conceptual Layout of Multidimensional Array http://msdn.microsoft.com/library/en...tm/vc38rc1.gif In declarations of multidimensioned arrays that have an initializer-list (as described in Initializers), the constant-expression that specifies the bounds for the first dimension can be omitted. For example: const int cMarkets = 4;The preceding declaration defines an array that is three rows by four columns. The rows represent factories and the columns represent markets to which the factories ship. The values are the transportation costs from the factories to the markets. The first dimension of the array is left out, but the compiler fills it in by examining the initializer. The technique of omitting the bounds specification for the first dimension of a multidimensioned array can also be used in function declarations as follows: #include <float.h> // Includes DBL_MAX.The function FindMinToMkt is written such that adding new factories does not require any code changes, just a recompilation. |
| ||
| Re: (reformatted) How to return Multi-Dimensional Arrays Thanks for the info. However, my question has not been anwered. The above I am already aware of and anything I have forgotten I can go find my textbook from university. What I can not find in that stupid book is how to return and array/pointer properly I've tried the new/delete and get conversion errors etc. This is what I need help with: returning a multidimensional array(probably through a pointer but how?) Again thank you. Tim |
| ||
| Re: (reformatted) How to return Multi-Dimensional Arrays Quote:
Try making a global variable of some sort & instead of using 'return' just have the later parts of the program access the global variable created by your script that you are trying to add 'return' to. |
| ||
| Re: (reformatted) How to return Multi-Dimensional Arrays This what I've done and get: I've moved array xyz_data[10][3][512] from being a private data member to a public data member of class scan_data. Called my member function(scan_data::add_image)(see above) that does the wanted conversion and stores that data into the now public xyz_data[10][3][512] array. In the main form of my project I've cut & pasted the code for my member function get_image_data to avoid having to pass and return my array. Below is that snippet: {the error I get is this: [C++ Error] Unit1.cpp(213): E2034 Cannot convert 'double ( *)[3][512]' to 'double *' [C++ Error] Unit1.cpp(230): E2062 Invalid indirection What am I doing wrong....I am not seeing something here that is probably obvious. Thanks |
| ||
| Re: (reformatted) How to return Multi-Dimensional Arrays Do I need to use the new/delete? thanks |
| ||
| Re: (reformatted) How to return Multi-Dimensional Arrays I am somewhat new to C++ (all programming for that matter) but what I figured out is that if I simply pass the array that I want to be changed as a parameter to the function that would have returned the multidimensional array I can have that function change the data to whatever I need it to be. Probably not what you are looking for and definitely not great programming but I just thought I would throw it out there. Ex. int (*data)[9][9]; void *getData(int (*dataToBeChanged)[9][9]) { dataToBeChanged = data; } As you can see above, 'data' is a pointer to a multidimensional array. After passing a multidimensional array of the same size to 'getData', the 'dataToBeChanged' referenced the same array. Good luck! Looks like your doing some pretty cool stuff. |
| ||
| Re: (reformatted) How to return Multi-Dimensional Arrays hey TJW iam doingthe exact same thing and i ran into the same problem so did you find any solution.. |
| ||
| Re: (reformatted) How to return Multi-Dimensional Arrays *Grumbles* |
| All times are GMT -4. The time now is 5:34 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC