LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass a string via a pointer to void in CVI

How can I pass a string via a pointer to void like shown in the sample.c attached? I tried everything but didn't have a success.
0 Kudos
Message 1 of 7
(3,520 Views)
Hello

Its not void * thats causing the problem. if you want to allocate the string from inside the function, you actaully need to pass it a char** instead of a char*. This way, you can actaully get back the memory block that malloc assigned to it. For GetVal though, a char* should work.
Another problem is that after assigning the string, you are freeing the memory block inside the function, making it unusable outside the function. You should free the memory block only after you are done using the string in your program.

Bilal Durrani
NI
Bilal Durrani
NI
0 Kudos
Message 2 of 7
(3,520 Views)
Hi,
As you wrote, I tried passing a char**, but didn't have a success, also with not deallocating memory it doesn't work. I am just absolutely unable to pass any pointer throught a void* parameter, anything else works also passing a string but not by passing only the pointer. I attached the changed sample.c in which way I tried to pass a pointer.
0 Kudos
Message 3 of 7
(3,520 Views)
I should have played with it some more. Problem is although you can pass anything as a pointer to void, you cant use the void for anything(makes sense). So unless you want to change the declaration to void**, you can try the following

void * ThreadFunction (void *functionData)
{
functionData = calloc (strlen("Test")+1, sizeof(char));

strcpy((char*)functionData,"Test");

return functionData;
}

where the usage is


char* One
...
One=(char*)ThreadFunction(One);
...
free(One)//after use of the string is done.

You could take in One and reallocate it as well by changing the calloc to realloc incase you are not sure whether the user has already allocated memory for this or not.

Hope this helps
Bilal
Bilal Durrani
NI
0 Kudos
Message 4 of 7
(3,520 Views)
Ok, your sample works but only because you have the variable "functionData" as return value too in the Function "ThreadFunction". If you only want to pass it via the parameter "functionData" the Variable "One" stays in the "uninitialized"-Status! I don't know if its a problem of the version of cvi, I'm working with measurement studio 6.
0 Kudos
Message 5 of 7
(3,520 Views)
Try this then.

void CVICALLBACK ThreadFunction (void *functionData)
{
char** test = functionData;

*test = calloc (strlen("Test")+1, sizeof(char));

strcpy(*test,"Test");

return;
}

Usage:

char *One;
.....

ThreadFunction(&One);
...
free(One);

Bilal
Bilal Durrani
NI
0 Kudos
Message 6 of 7
(3,520 Views)
This worked!!!!!!!! Great!! Thanks for your help!
0 Kudos
Message 7 of 7
(3,520 Views)