LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

"an array of string"

Hello!
I hope my message is not stupide but I am really blocked.
I want to initialize an array of string and I don't succed in.This array will be sent by a data sucket so among the DS_SetDataValue help I must ahve a char * value [ ];
This help give this exemple:
 
Example of 1D array of strings
char *value[] = {"String Value 1", "String Value 2"};
DS_SetDataValue (dsHandle, CAVT_CSTRING | CAVT_ARRAY, value, sizeof (value) / sizeof (value[0]), 0);
 
But I want to allocate dynamically the char *value[ ]:
Code I use is that one:
{
char * value [50]; 
int tempi;
double tempd;
 
memset(value,1,50);
 
tempi=12;
Fmt(value[0],"%d",tempi);
 
tempd=1.568;
Fmt(value[1],"%f",tempd);
 
ect....
 
DS_SetDataValue (SendDataContexte, CAVT_CSTRING | CAVT_ARRAY, value, sizeof (value) / sizeof (value[0]), 0);
}
 
 
If someone have an idea??
0 Kudos
Message 1 of 5
(3,724 Views)
OUPS I forgot to say that the code I use don't work because when the line "Fmt(value[0],"%d",tempi);" is executed all the strings in the array are at the same value i.e. "tempi".And it's the same for all the Fmt Function
0 Kudos
Message 2 of 5
(3,717 Views)

You might want to look again at your declaration char * value [50];  I think what you intended was a single array of 50 characters, which would be declared as char value [50];  your declaration generated 50 pointers to characters - this is completely different.

JR

0 Kudos
Message 3 of 5
(3,711 Views)
Hello Jr2005!
 
Thanks a lot for your help but I declare my array of 50 string as an array of 50 string ( char * value [50] )
I join my screen schot so that you can see my problem.
Best regards
Paul.
0 Kudos
Message 4 of 5
(3,707 Views)
No, with all respect, in your example you declare 52 pointers of type char, like jr_2005 said. The memory to which these pointers point is not allocated which is very dangerous and may lead to undefined behaviour which I think you see ('12' in all cases).  
 
To declare 50 strings of length 20 you would do this:
 
char ArrOfStr[50][20];
 
This allocates a block of 1000 bytes fixed in memory.
 
 
But how you started, by declaring 52 char-pointers is good. Only then you should use malloc( ) to allocate memory for each string when you want to use it (and know how much space you need). This way you dynamically allocate memory and use your memory more efficient than in my previous example. This would look something like:
 
char *ArrOfStr[52];
int MyNumber;
:
:
/* => assuming the integers you use run from 0-999, so in string form are 3 bytes long */
ArrOfStr[0] = (char *) malloc( 4 * sizeof(char) );    /* allocate 4 bytes */
 
if ( ArrOfStr[0] != NULL )
{
sprintf( ArrOfStr, "%d", MyNumber );
}
else
{
/*error*/
}
 
 
Now, I did not compile this it may contain typo's, but the general idea should be clear.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Message 5 of 5
(3,697 Views)