LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Array

hello,
I has small problem and does not find out the solution at the moment, because the solution is  sure simple.
I would like to give values in a panel and then i would store this values in an array. but the function always stores only the first values.
can somebody, please help me?

thanks

 



STRUCT_VALUE WriteArray( STRUCT_VALUE ARRAY[ 10000 ]  )
{
      STRUCT_VALUE tmpValue;
 
       //read values out from panel
      GetCtrlIndex( panelHandle,  PANEL_PHASE,       &tmpValue.i16Phase );
      GetCtrlVal(     panelHandle, PANEL_TIMER,        &tmpValue.i16Timer );
      GetCtrlVal(     panelHandle, PANEL_COUNTER, &tmpValue.i32Counter );
      GetCtrlVal(     panelHandle, PANEL_VOLTAGE,  &tmpValue.i32Voltage );
      GetCtrlVal(     panelHandle, PANEL_TEMP,         &tmpValue.i16Temp);
 
      //give array back
      return( tmpValue );
}

 

0 Kudos
Message 1 of 6
(3,925 Views)

Hello Axinia,

you have already posted this same question in this thread: as far as I can understand the situation has not changed since dummy-decoy comments, so I want to summarize them here asking you to consider them and modify your code accordingly.

 

1. You are passing the function an array but you are not using it

2. GetCtrlIndex and GetCtrlVal both return single values, not arrays of values (how they could?) so it's not clear how you indend to fill your array

3. You are returning a single structure, so the best you can obtain is that this structure fills one element of a corresponding structure array

 

More generally, querying a control on a panel returns the value the control has at this moment: CVI does not have control arrays nor arrays of controls, so the only way to fill several array elements is to address individually each of them querying the corresponding control.

 

If you can give us some more elements on what are intending to do we can try giving you appropriate suggestions and hints on how to accomplish your tasks.

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 6
(3,919 Views)

i am so stupid. sorry I have copied the wrong code.

 

if I insert following code  comes: FATAL RUN-TIME ERROR: "speicher.c", line 115, col 9, thread id 0x00000C0C:   The program has caused a 'General Protection' fault at 001B:00402545.

 

the variables has following values at this error

Name                  Value
----------------------------------
arrayIndex             4
arraySize             56


but if i set arraySize = 4, it always reads to me only the first value, the rest of array is fill with 0 

 

STRUCT_VALUES WriteArrayFile( STRUCT_VALUES ARRAY[ 10000 ]  )
{
           int arrayIndex = 0;
           int arraySize = ( sizeof( ARRAY[ 10000 ] ) );
 
          while( arrayIndex < arraySize )
          {
                   //insert a new phase and set index
                   InsertPanelPhase( );
                   SetCtrlIndex( panelHandle, PANEL_Phase, arrayIndex+1 );
   
                    //read values out from panel
                   GetCtrlIndex( panelHandle, PANEL_PHASE, &ARRAY.i16Phase );
                   GetCtrlVal( panelHandle, PANEL_TIMER, &ARRAY.i16Timer );
                   GetCtrlVal( panelHandle, PANEL_COUNTER, &ARRAY.i32Counter );
                   GetCtrlVal( panelHandle, PANEL_VOLTAGE, &ARRAY.i32Voltage );
                   GetCtrlVal( panelHandle, PANEL_TEMP, &ARRAY.i16Temp);
 
                   arrayIndex++;
               }
           //give array back
          return( *ARRAY );

0 Kudos
Message 3 of 6
(3,884 Views)

i see some problems:

 

1. read the documentation for the sizeof() operator: sizeof() returns the size in BYTES, but at each index of your array you have a very big structure (STRUCT_VALUES seems to be at least 20 bytes), so sizeof( ARRAY ) would return something like 10000*20. the number of elements in the array is:

int arraySize = sizeof( ARRAY ) / sizeof( *ARRAY );

sizeof( *ARRAY ) returns the size of the first element in the array in bytes.

 

2. unfortunately, you wrote: 

int arraySize = ( sizeof( ARRAY[ 10000 ] ) );

ARRAY is a variable, not a type. ARRAY[10000] is the 10001th element of the array (remember that indexes starts at 1). this is 1 element past the end of the array, which may trigger a GPF or a debugger message (but would probably not here as the expression is evaluated at compile time). also, the size of ARRAY[10000] is the size of 1 element in the array, not the size of the whole array.

 

3.  when reading the controls on the panel, you wrote:

GetCtrlIndex( panelHandle, PANEL_PHASE, &ARRAY.i16Phase );

look at the last parameter. where do you think the value is stored ? more specifically, into which element of the array do you think the value is stored ?

remember, once again, that in order to store something into an array, you should specify an index into the array. (an index is specified using square brackets '[ ]')

 

4.now here is your problem:

return( *ARRAY );

this is consistent with the declaration of the function:

STRUCT_VALUES WriteArrayFile( STRUCT_VALUES ARRAY[ 10000 ]  );

your function does not return an array but a single structure,and your return statement does only return the first element of your array.

(here is the answer to this one: declare your function as 

STRUCT_VALUES * WriteArrayFile( STRUCT_VALUES ARRAY[ 10000 ]  );

and write your return as:

return ARRAY;

now, you can't say we are not helping...)

 

last thing, now that you are making some progress writing in C, you MUST ABSOLUTELY learn how to use the debugger. knowing how to debug a program is a VITAL part of programming, and learning this skill very early when programming allows to get some understanding of how a program works. CVI's debugger is very complete and you should take some time to learn how it works. for example, point 1 and 2 above would have been obvious when observing the value of "arraySize" in the debugger.

0 Kudos
Message 4 of 6
(3,876 Views)

thx dummy_decoy

 

but if afterwards I the function appeal  comes following error:  Dereference of out-of-bounds pointer: 1 bytes (1 elements) past end of array.


//global variables

STRUCT_VALUES *arrayWrite[  10000  ];
STRUCT_VALUES ARRAY;

 

 

int CVICALLBACK GenerateArrayCallback (int panel, int control, int event,  void *callbackData, int eventData1, int eventData2)
{
            switch (event)
             {
                   case EVENT_COMMIT:
                                 arrayWrite[  10000 ] = WriteArrayFile( &ARRAY );
                          break;
              }
         return 0;
}

0 Kudos
Message 5 of 6
(3,849 Views)

Well, here several problems can arise.

 

1. with  STRUCT_VALUES *arrayWrite[  10000  ]; you are defining an array of pointers to structures. To have an array of structures you must remove the asterisk ( STRUCT_VALUES arrayWrite[10000]; ). Elements of THIS array can be addressed with arrayWrite[index].element.

2. ARRAY is a variable of type STRUCT_VALUES, not an array. You are passing to the function a pointer to a variable, not an array.

3. Your functions returns (supposedly) an array (?) but tries to copy it on a single array element which does not exists (an array of 10000 elements has indexes for 0 to 9999 ! )

 

It seems to me that there is a lot of confusion about arrays and pointers: reading a good C reference could clarify this matter.

Message Edited by Roberto Bozzolo on 04-23-2009 12:17 PM


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 6
(3,834 Views)