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
 vector of user-defined 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 - 01/23/2008 :  5:22:01 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hello,

In Origin 8, is it possible to declare vector of user-defined structure ?

Thank you,
Pierre.

cpyang

USA
1406 Posts

Posted - 01/24/2008 :  04:47:49 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Sorry, Origin 8 does not have much change in terms of Origin C.

You have to use malloc and free, but can wrap in a nice class, as shown below:
 
struct myTest {
double a;
int n;
};

class MyTestVector
{
public:
MyTestVector(int nn)
{
m_nSize = nn;
m_p = (myTest*) malloc(m_nSize * sizeof(myTest));
}
~MyTestVector()
{
free(m_p);
}
int GetSize() {return m_nSize;}
myTest* GetData() {return m_p;}
myTest* GetAt(int n) {
if(n >=0 && n < m_nSize)
return m_p[n];

return NULL;
}
private:
int m_nSize;
myTest* m_p;
};


void test(int nn = 5)
{
MyTestVector junk(nn);

for(int ii = 0; ii < junk.GetSize(); ii++)
{
myTest* pp = junk.GetAt(ii);
pp->a = ii*0.01;
pp->n = ii;
}

for(ii = 0; ii < junk.GetSize(); ii++)
{
myTest* pp = junk.GetAt(ii);
printf("val=%g, index = %d\n", pp->a, pp->n);
}
}




CP


Go to Top of Page

pierremx

Belgium
Posts

Posted - 01/24/2008 :  05:51:47 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thank you for your help.
In my case I will use malloc...

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