LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with GetCtrVal of a string

Hi, I've created a ring and I'm trying to get the string in this ring. My code is:
 
 char string;
 
   GetCtrlVal (panel, PANEL_RING_1,&string);
   printf("%s",string);
  
 
but when I'm trying to print, it's said "expecting pointer to char but found int" , can someone help me?
Thanks.
0 Kudos
Message 1 of 8
(4,956 Views)

Hi Dara,

to retrieve a stringe input into a string / ring control you need an array of chars, not a single char which is actually a single character / byte value. Second, the name of a string array is itself a pointer to the first element of the array and so a pointer to the whole string.

Given this, I suggest you modify your code as follows:

char string[256];       // Size the string to your actual needs
 
   GetCtrlVal (panel, PANEL_RING_1, string);     // You don't need the '&' character
   printf("%s", string);
 
 
Hope this helps
Roberto


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?
Message 2 of 8
(4,949 Views)

Typically you know how long the text in the ring control should be (so you make your character array variable atleast that long plus one--for the null byte).  If you don't have any clue how long it will be then you can do it another way, but it is much more complicated than just picking some really big 'safe' length.  Unfortunately, if you use the 'safe' length and then some user comes along and types something in that is longer than your program is prepared for, then the 'safe' length won't be safe anymore.  There is a whole class of software exploits that take advantage of fixed length string buffers.  This should work better (99.99% of the time).

char *string;
int length, index;

GetCtrlIndex(panel, PANEL_RING_1, &index);
GetCtrlValStringLength(panel, PANEL_RING_1, &length);
string = malloc(sizeof(char) * (length + 1));
GetCtrlVal(panel, PANEL_RING_1, string);
printf("%s", string);
free(string);

Message 3 of 8
(4,947 Views)

You will also find useful AllocateCtrlValString  function that does allocate the exact string for retrieving control text. Be sure to free the string after you have used it and to pass a correct control type to the function, otherwise you will receive error -10 (control is not of the type expected by the function) and the string will remain unchanged. You can use this function as follows:

int   error;

char  *string = NULL;

error = AllocateCtrlValString (panel, impo_list, &string);

// "string" contains control text

if (string) free (string);

 

Unfortunately you cannot use neither AllocateCtrlValString nor GetCtrlValStringLength on rings as their data type is numeric type (integer, double...) while these functions expect a datatype of char.

Message Edited by Roberto Bozzolo on 12-13-2005 09:15 AM



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?
Message 4 of 8
(4,933 Views)
Hey Roberto,
  The AllocateCtrlValString() looks kinda snazzy.  I will have to check into it.  However, I disagree with the 'Unfortunately you cannot...' comment.  There are text ring controls.  You must set the ring data type to char *, and thus the values returned by GetCtrlValStringLength() do, in fact, return string lengths and GetCtrlVal() returns the string.  If you are programatically inserting list items, like I often do, then you might use a line like:
 
InsertListItem(panel, PANEL_RING_1, -1, "Test", "Test");  //-1 inserts item at end of list
 
  If you run the demo code that I submitted immediately after this, you will see that it returns a proper string length and value.  It works just as well if you add the items to the listbox in the panel before it is compiled.
 
Orlan
Message 5 of 8
(4,904 Views)

Orlan, you're right that a ring control can have a char * datatype: it hasn't come to my mind since I use this option very rarely, but indeed it's true. Thanks to have pointed us to this facility.

Roberto



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?
Message 6 of 8
(4,899 Views)

Hey Dara,

  Hopefully you have more than enough input there! Smiley Very Happy  It's actually quite difficult to catch Roberto off guard.  He's quite helpful on the board, to me as well, on numerous occasions.  Please post again and any of us will do our best to help you out.

Good luck,

Orlan

Message 7 of 8
(4,875 Views)

Hi , thank you very much for all your help Orlan and Roberto.

Your advices have been very helpfull and hope I could reach your level one day Smiley Wink

See U.

0 Kudos
Message 8 of 8
(4,828 Views)