LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

What is wrong with this passing a string reference?

I try to send a reference string to my function and hope it will change the content as desired, but it is not! Here how my bad codes are:
 
int PassStringReference(char *str_ref)
{
     str_ref = "return";
     return 0;
}
 
void main(void)
{
       int error;
       char *str="string refrence";

       error = PassStringReference(&*str);
       printf("%s", str);
}
The result str = "string reference"???
 
Can someone show me what I did incorrcetly? By the way, I need to return an integer for other purpose, can not use return string in this case!
 
Thanks,
0 Kudos
Message 1 of 5
(3,440 Views)
In your syntax
str_ref = "return";
you are creating a string constant.  You should use strcpy instead.
strcpy (str_ref, "return");
 
Your syntax
PassStringReference(&*str);
is the same as
PassStringReference(str);
Message 2 of 5
(3,435 Views)

Hello

 

You should try the following:

 

void main(void)

{

int error;

char str[30]="string reference";  // make sure 30 char is always enough

error = PassStringReference(str);

...

}

 

Regards,

0 Kudos
Message 3 of 5
(3,434 Views)

Your suggestion strcpy is working very well. However, I did try with previous codes with 2 variable strings (for easy showing my problem: I described with constant strings) and it did not work???

Many thanks Smiley Happy

0 Kudos
Message 4 of 5
(3,426 Views)

I did try but it is the same result, but the other suggest method is working well

Thanks anyway Smiley Wink

0 Kudos
Message 5 of 5
(3,425 Views)