473,386 Members | 1,803 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,386 software developers and data experts.

indexing elements of a 2D array by pointer for transpose

My forehead is flat from pounding.

I am building a DLL in VS2005 C++ for use in another software
development platform.

I am required to pass my array data in/out of the function via a
pointer, in this case, to double.

The simplest way for me to transpose the array is via addressing its
individual elements in the conventional form such as

array2dt[i,j] = array2d[j,i];

where both variables appear in the call list as

long redimSTr(..., double *array2d, double *array2dt)

I realize that the algorithm looks like it will likely only work for a
square matrix, but since array2dt is coming back completely unchanged,
I suspect that there is something else fundamentally wrong with my
logic, specifically, the way to directly address individual elements
of a multi dimensional array represented by a pointer in a function.

As you may suspect, please don't assume I'll just catch a hint.
'splain to me, Lucy!

tanks

pc
Aug 5 '08 #1
4 4008


pc_whocares wrote:
My forehead is flat from pounding.
....
As you may suspect, please don't assume I'll just catch a hint.
'splain to me, Lucy!
Without more code, no-one will be able to help you.
Aug 5 '08 #2
On Aug 4, 6:06 pm, pc_whocares <pc_whoca...@yahoo.comwrote:
My forehead is flat from pounding.

I am building a DLL in VS2005 C++ for use in another software
development platform.

I am required to pass my array data in/out of the function via a
pointer, in this case, to double.

The simplest way for me to transpose the array is via addressing its
individual elements in the conventional form such as

array2dt[i,j] = array2d[j,i];

where both variables appear in the call list as

long redimSTr(..., double *array2d, double *array2dt)

I realize that the algorithm looks like it will likely only work for a
square matrix, but since array2dt is coming back completely unchanged,
I suspect that there is something else fundamentally wrong with my
logic, specifically, the way to directly address individual elements
of a multi dimensional array represented by a pointer in a function.

As you may suspect, please don't assume I'll just catch a hint.
'splain to me, Lucy!

tanks

pc
Thanks for your replies. Let me try again.

I need a way to manipulate a 2D array in a subroutine. What's the
magic thing that will allow me to go from a pointer and base type to a
2D array? I am obviously a bare novice here.

It seems that one cannot dynamically create a 2D array.

tanks

pc
// redim.cpp : Defines the entry point for the DLL application.
//
// Use the defined keyword DLLEXPORT in front of C/C++ functions
// that are meant to be exported for use in calling context
//
#include <stdlib.h>
#include "stdafx.h"

#ifdef WIN32
#ifdef __cplusplus
#define DLLEXPORT extern "C" __declspec(dllexport)
#else
#define DLLEXPORT __declspec(dllexport)
#endif
#else
#define DLLEXPORT
#endif

typedef double* dblArrayPtr;

DLLEXPORT long redimSTr(long n, long m, long arraySize, double SF,
double *array, double *array2d)

{
long i;
long j;

dblArrayPtr tmpArray;
// both errors next executable line
// -- apparently can't dynamically create 2D array
// Error 1 error C2540: non-constant
// expression as array bound
// Error 2 error C2440: '=' :
// cannot convert from 'double (*)[1]' to 'dblArrayPtr'
tmpArray = new double[n][m];

// from calling context array2d is m x n
// next for loop simply copies data from
// 1D "array" to 2D "array2d" and scales it
for (i = 0; i < arraySize; i++, array++, array2d++)
{
*array2d = *array * SF;
}

// intention here is to put scaled data into
// an array that I can address by row, column
tmpArray = array2d;

// perform transpose.. will only work for square matrix,
// but that's the least of my worries
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
tmpArray[i,j] = array2d[j,i];
}
}

// put transposed data back so I can access
// it from calling function
array = tmpArray;
// clean up
delete [] tmpArray;
// arraySize is unused upon return
return(arraySize);
}
Aug 5 '08 #3
On Aug 5, 10:32*am, pc_whocares <pc_whoca...@yahoo.comwrote:
On Aug 4, 6:06 pm, pc_whocares <pc_whoca...@yahoo.comwrote:
My forehead is flat from pounding.
I am building a DLL in VS2005 C++ for use in another software
development platform.
I am required to pass my array data in/out of the function via a
pointer, in this case, to double.
The simplest way for me to transpose the array is via addressing its
individual elements in the conventional form such as
* * array2dt[i,j] = array2d[j,i];
where both variables appear in the call list as
* *long redimSTr(..., double *array2d, double *array2dt)
I realize that the algorithm looks like it will likely only work for a
square matrix, but since array2dt is coming back completely unchanged,
I suspect that there is something else fundamentally wrong with my
logic, specifically, the way to directly address individual elements
of a multi dimensional array represented by a pointer in a function.
As you may suspect, please don't assume I'll just catch a hint.
'splain to me, Lucy!
tanks
pc

Thanks for your replies. *Let me try again.

I need a way to manipulate a 2D array in a subroutine. *What's the
magic thing that will allow me to go from a pointer and base type to a
2D array? *I am obviously a bare novice here.

It seems that one cannot dynamically create a 2D array.

tanks

pc

// redim.cpp : Defines the entry point for the DLL application.
//
// Use the defined keyword DLLEXPORT in front of C/C++ functions
// that are meant to be exported for use in calling context
//
#include <stdlib.h>
#include "stdafx.h"

#ifdef WIN32
#ifdef __cplusplus
#define DLLEXPORT extern "C" __declspec(dllexport)
#else
#define DLLEXPORT __declspec(dllexport)
#endif
#else
#define DLLEXPORT
#endif

typedef double* dblArrayPtr;

DLLEXPORT long redimSTr(long n, long m, long arraySize, double SF,
double *array, double *array2d)

{
* * * * long i;
* * * * long j;

* * * * dblArrayPtr tmpArray;
* * * * // both errors next executable line
* * * * // * * *-- apparently can't dynamically create 2D array
* * * * // Error * * * *1 * * * error C2540: non-constant
* * * * // * * *expression as array bound
* * * * // Error * * * *2 * * * error C2440: '=' :
* * * * // * * *cannot convert from 'double (*)[1]' to 'dblArrayPtr'
* * * * tmpArray = new double[n][m];

* * * * // from calling context array2d is m x n
* * * * // next for loop simply copies data from
* * * * // 1D "array" to 2D "array2d" and scales it
* * * * for (i = 0; i < arraySize; i++, array++, array2d++)
* * * * {
* * * * * * * * *array2d = *array * SF;
* * * * }

* * * * // intention here is to put scaled data into
* * * * // an array that I can address by row, column
* * * * tmpArray = array2d;

* * * * // perform transpose.. will only work for square matrix,
* * * * // but that's the least of my worries
* * * * for (i = 0; i < n; i++)
* * * * {
* * * * * * * * for (j = 0; j < m; j++)
* * * * * * * * {
* * * * * * * * * * * * tmpArray[i,j] = array2d[j,i];
this line does not do what you think it does. It actually does
tmpArray[(i,j)] = array2d[(i,j)]

* * * * * * * * }
* * * * }

* * * * // put transposed data back so I can access
* * * * // * * *it from calling function
* * * * array = tmpArray;
* * * * // clean up
* * * * delete [] tmpArray;
* * * * // arraySize is unused upon return
* * * * return(arraySize);

}

Aug 5 '08 #4
In C++, for an array having multiple dimensions, two adjacent elements in
the last dimension of the array are arranged contiguously in memory *, and
you work back from that.

* I say contiguously - but this will include allowances for issues such as
structure padding and alignment etc. The important point is that the
elements are effectively "next to each other" in memory.

You *must* know the dimensions of the array.

So an array like a[5,10] will have p0=a[0,0], p1=a[0,1], p2=a[0,2], ...
p10=a[1,0] etc, where p0 is a memory location, and p1 is the next memory
location etc. (Next contiguous memory location based on the SIZE of the
elements, that is).

You can write a simple class to handle the access to a multi-dimensional
array, so it does the conversions for you.

For example (I am writing this off the top of my head, so it may not compile
cleanly):

template< class T >
class TwoDimArray
{
public:
TwoDimArray( T* _Buffer, int _FirstDimension, int _SecondDimension )
{
m_Buffer = _Buffer;
m_FirstDim = _FirstDimension;
m_SecondDim = _SecondDimension;
}

T& Item( int d1, int d2 )
{
// you could do assertions etc here to check that d1, d2 are
"in-range"

int sequentalPosition = ( d1 * m_SecondDim ) + d2;
return m_Buffer[ sequentialPosition ];
}

T* m_Buffer;
int m_FirstDim;
int m_SecondDim;
};

int main( )
{
double* buffer = new buffer [ 100 ];

TwoDimArray arr( buffer, 10, 10 );

arr.Item( 0, 0 ) = 100.0;
arr.Item( 1, 7 ) = 200.0;
}
Aug 6 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
19
by: Steven T. Hatton | last post by:
I believe it is technically possible to return a pointer to the first element of an array. I can persuade the returned pointer to act like an array, with some qualifications. I'm specifically...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
6
by: Azdo | last post by:
Hello, if i wanted to access the elements of an array which is owned by an auto_ptr, as: #include <memory> int main(){ std::auto_ptr<int> p(new int); p=2; //Error: no operator defined
18
by: Joshua Neuheisel | last post by:
The following code compiles with gcc 3.2.2 and Visual C++ 6: #include <stdio.h> int main() { int a = {3, 4}; printf ("%d\n", 0); return 0; }
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
9
by: Jonathan Burd | last post by:
Greetings everyone, I have noticed a little quirk in C's array-indexing syntax. I wonder what practical purpose this little "inverting" serves to be included in the standard (it compiles fine...
10
by: Tamir Khason | last post by:
I have a pointer to array and I want to apply indexing to this Array so I have function (name it IntPrt Func) to go to certain member I can use (as C++) Func, but in C# I recieve an error "Cannot...
6
by: ditiem | last post by:
I hope this is the correct group. It came out a doubt about speed when indexing an array in the following way: for a given pointer p, p contains an address and p an offset. I must return the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.