LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Struct Use

Dear alls,
 
I'm a new user in Labwindowsand I've problem with the Structs,
 
I would like to make (and use) a struct
 
typedef struct
 {
 char mau[30];
 double valor;
 } estructura;  

estructura *estruc[10];
 
 
But I don't know what happens, It isnot working properly.
 
 
I would like to define a struct with
 
double valor and char mau[30].
 
But after to this, I need 10 times of this struct (I don't know if Is it clear?).
 
For example, Is it possible to use:
 
estruc[1]->mau="Test";
estruc[1]->valor=123.12;
estruc[2]->mau="Test2";
estruc[2]->valor=999.99;
 
 
Do I need the
0 Kudos
Message 1 of 4
(3,893 Views)
Prado -

Well, you've declared an array of pointers to struct estructura, but you haven't created the structs themselves and initialized the pointer array.  Your array of type "pointer to struct estructura" will work as you want, but only if you separately create the structs themselves, and then initialize the array with pointers to the structs.  Depending upon how you want to do it, you might separately declare each struct as a local and then initialize the array as in:

estructura struct1, struct2, struct3, ... ;
estruc[0] = &struct1;
estruc[1] = &struct2;
...
estruc[0]->mau = "Test";
estruc[0]->valor = 123.12;
...

Or, you could malloc() memory to the proper size for your structs and dereference the returned pointer to initialize.  Pointers and arrays are essentially the same thing in C:  you could use array notation to initialize the malloc'd memory if you wanted.

Alternatively, you could create an array of type estructura (the structs themselves, not simply pointers to the structs).  Then you would initialize each array member

estructura    estruc[10];

estruc[0].mau = "Test";
estruc[0].valor = 123.12;
estruc[1].mau = "Test2";
estruc[1].mau = 999.99;

...

And finally, you can pass structs as parameters either by value or by reference.  Of course if you pass by value, the called routine gets a local copy of the struct, and changes to it won't change the original.

Hope this helps.

Bob
0 Kudos
Message 2 of 4
(3,880 Views)

Prado,

Do you actually want to create a struct or do you want to create an array?

Using a struct will not result in this:

estruc[1]->mau="Test";
estruc[1]->valor=123.12;
estruc[2]->mau="Test2";
estruc[2]->valor=999.99;

It will result in this:

typedef struct
{
 char mau1[4];
 int valor1;
 char mau2[5];
 int valor2;
} _estruct_name;

The data is then accessed by doing this:

estruct_name.mau1

notice the dot separating the structure name and the value you are looking for.

That is what I get the impression that you want to create arrays to store different values within the same valiable name, such as

mau[array_index]="test"

valor[array_index]=123.12

Example:

mau[0] = "test1"

mau[1] = "other test"

mau[2] = "some other title"

valor[0] = 123.12

valor[1] = 333.22

valor[2] = 543.12

So that when you index the array, you can associate values for the same iteration, such as:

test1                         other test                               some other title

123.12                      333.22                                   543.12

 

Is what I am describing what you are looking for??

JLV

😄

 

0 Kudos
Message 3 of 4
(3,869 Views)

I really need a course on speed reading... 😉

Also see what Roberto has to say here:

http://forums.ni.com/ni/board/message?board.id=180&message.id=17383

And please disregard my previous post...

JLV

Message 4 of 4
(3,846 Views)