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
 All Forums
 Origin Forum for Programming
 Forum for Origin C
 2-dimensional array of structure

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
pierremx Posted - 03/12/2009 : 5:11:21 PM
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.
2   L A T E S T    R E P L I E S    (Newest First)
pierremx Posted - 03/13/2009 : 10:46:46 AM
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.
cpyang Posted - 03/12/2009 : 8:35:00 PM
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.


The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000