The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 2-dimensional array of structure
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

pierremx

Belgium
Posts

Posted - 03/12/2009 :  5:11:21 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 7.5 / 8
Operating System: XP

I needed to use 2 dimensional arrays in Origin C.
The multidimensional C array type notation Tab[x][y] don't work in origin C.
Below, my solution.

Do you have another solutions?

#include <Origin.h>

struct stTemp
{
int a;
int b;
};

void test(struct stTemp **tt)
{
int i,j;
struct stTemp *ptr = NULL;

// Initialize b member of array
for (i = 0; i < 3; ++i)
{
ptr = *(tt + i);
for (j = 0; j < 2; ++j)
ptr[j].b = i * 200 + j;
}


// Show all the array
for (i = 0; i < 3; ++i)
{
ptr = *(tt + i);

for (j = 0; j < 2; ++j)
printf(" (%i,%i) [a] = %i [b]= %i ", i,j,ptr[j].a,ptr[j].b);
printf("\n");
}
}

void main()
{
// 2 dimensional array of structure
// t[3][2]

struct stTemp *t[3];
struct stTemp t0[2];
struct stTemp t1[2];
struct stTemp t2[2];

t[0] = &t0[0];
t[1] = &t1[0];
t[2] = &t2[0];

// initialize a member of array
t[0][0].a = 1;t[0][1].a = 10;
t[1][0].a = 2;t[1][1].a = 20;
t[2][0].a = 3;t[2][1].a = 30;

// initialize b member of array and show it
test(&t[0]);
}


Regards,
Pierre.

cpyang

USA
1406 Posts

Posted - 03/12/2009 :  8:35:00 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You can use pointer and store into a matrix, as shown below:

struct stTemp
{
	int a;
	int b;
};

void test()
{
	stTemp* pst;
	matrix<uint> mu(3,4);
	int ii, jj;
	
	for(ii = 0; ii < mu.GetNumCols(); ii++)
	{
		for(jj = 0; jj < mu.GetNumRows(); jj++)
		{
			pst = new stTemp;
			pst->a = (jj+1)*10;
			pst->b = ii+1;
			mu[jj][ii] = (uint)pst;
		}
	}
	
	for(ii = 0; ii < mu.GetNumCols(); ii++)
	{
		for(jj = 0; jj < mu.GetNumRows(); jj++)
		{
			pst = (stTemp*)mu[jj][ii];
			printf("mu[%d][%d], a=%d, b=%d\n", jj, ii, pst->a, pst->b);
		}
	}
	
	//cleanup
	for(ii = 0; ii < mu.GetNumCols(); ii++)
	{
		for(jj = 0; jj < mu.GetNumRows(); jj++)
		{
			pst = (stTemp*)mu[jj][ii];
			delete pst;
			mu[jj][ii] = 0;
		}
	}
}


Best will be to put the new and delete code inside a class so that you can manage the 2D pointers with constructor/destructors.

Go to Top of Page

pierremx

Belgium
Posts

Posted - 03/13/2009 :  10:46:46 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi cpyang,

Yes, it is a good idea!
In Origin 8, there is the "Array" possibility (search array in the help) but I don't directly understand how to use it.

Regards,
Pierre.
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000