LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

when does a while loop start

I am tryint to dynamically build and populate an array of strings. I can define a condition that will create the array and populate the first element, but subsequent updates of the array are not allowed because I have to loop the new array back into the beginning of the while loop (with a switch function) but that is not allowed. I would like an elegant way of doing this
0 Kudos
Message 1 of 4
(3,037 Views)
I'm not sure what exactly you're trying to accomplish. If you create an array with zero elements, you can't replace elements that aren't there. I've attached a VI that creates a string array the same size as the input boolean array and every place the boolean array contains a true constant, it inserts the index of the context into the string array. If that's not what you're attempting to do, please explain further.
0 Kudos
Message 2 of 4
(3,037 Views)
Hi Konrad,
as Dennis suggests I'd really need a little bit more info, such as the programming environment, C or C++, etc. Below is a worst case C possible solution that dynamically allocates an array of dynamically allocated char* strings. This might solve your problem, but as always treat other people's code with suspicion as I've not really tested it. C++ has alot more toys in CString from memory.
Best Regards
Ken



#include
#define TRUE 1
#define FALSE 0
#define MAXSTRING 20 // think hard about this one
#define YOURNUMBEROFSTRINGS 6
int hello(void);
int main()
{

hello();
}

int hello(void)
{

char** array;
int i =0;
char yourstring[MAXSTRING] = "hello";
int more = TRUE;
int stringCounter =0;

array = mal
loc (sizeof(char*)*YOURNUMBEROFSTRINGS);

while (more)
{
array[i] = malloc (sizeof(yourstring)); // allocate enough memory
strcpy(array[i],yourstring);// remember to terminate your strings if not already done so
i++;
if (i== YOURNUMBEROFSTRINGS) // loop exit condition of your choice goes here (eg out of strings)
{
more = FALSE;
}
}
stringCounter = i;
// dont forget to free dynamic memory allocation
i =0;
while (i {
free(array[i]);
i++;
}
free(array);
return 0;
}
0 Kudos
Message 3 of 4
(3,037 Views)
You say "subsequent updates of the array are not allowed because I have to loop the new array back into the beginning of the while loop...but that is not allowed." That's not completely correct. You can't wire a loop output externally back to a loop input, but you can do it internally to the loop by using shift registers. The shift registers keep data internal to the loop until the loop is done executing, and they allow you to update the array (or whatever they're wired to) from one iteration to the next. You can't wire a loop output to a loop input externally to the loop because the loop won't start until it receives all of its inputs, but it won't provide any outputs until it's done. So it can never start.
Dennis's example uses shift registers. Review the LabV
iew help topics under "Shift Registers" in the index for more info.
0 Kudos
Message 4 of 4
(3,037 Views)