473,705 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4037


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...@ya hoo.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(dlle xport)
#else
#define DLLEXPORT __declspec(dlle xport)
#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(arraySiz e);
}
Aug 5 '08 #3
On Aug 5, 10:32*am, pc_whocares <pc_whoca...@ya hoo.comwrote:
On Aug 4, 6:06 pm, pc_whocares <pc_whoca...@ya hoo.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(dlle xport)
#else
#define DLLEXPORT __declspec(dlle xport)
#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(arraySiz e);

}

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 _SecondDimensio n )
{
m_Buffer = _Buffer;
m_FirstDim = _FirstDimension ;
m_SecondDim = _SecondDimensio n;
}

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

int sequentalPositi on = ( d1 * m_SecondDim ) + d2;
return m_Buffer[ sequentialPosit ion ];
}

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
2745
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 (i.e., a matrix) it seems to be trivial, with array indexing, to extract a subset of its *columns*. But it does not seem to be trivial to extract a subset of its *rows*. The code snippet below describes the problem (if it really is a problem)...
19
1931
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 interested in multidimensional arrays. It is often said that arrays and pointers are virtually identical. My observations are that my (gcc) compiler knows the difference between T*, T a1, and a2. What I'm currently trying to do is to return a...
11
2741
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
1517
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
5400
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
4149
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 end is found is less efficient than using sizeof operator , since size of the array is completely determined at compile time. i don't quite understand this. Could anyone explain to me in detail ?
9
1776
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 with gcc -pedantic -Wall -std=c89 and executes properly when run). The code follows: #include <stdio.h>
10
4648
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 apply indexing with to an expression of type 'System.IntPtr'". How to get rid of it? TNX --
6
1406
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 content of the address plus the offset.. Which way is faster and more portable? 1.- *( ((unsigned int*)*p) + *(p+1) ) ; 2.- ((unsigned int*)*p) ; 3.- *((unsigned int*)(*p ++ + *p)) ;
0
8767
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8689
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9273
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7893
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6605
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5933
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4703
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.