return String from C to XC?

Technical questions regarding the XTC tools and programming with XMOS.
jerryXCORE
Experienced Member
Posts: 65
Joined: Tue Apr 30, 2013 10:41 pm

return String from C to XC?

Post by jerryXCORE »

Just wondering if it is possible to pass 'string' argument back and forth between C and XC?

For example:
Define function In C:

Code: Select all

void func(char str1[])
{     printf("Old:  %s", str1);    //Old: old string
      str1 = "new string";
 };
Call the function from XC:

Code: Select all

      char str1[100] = "old string";
      func(str1);
      printstr("New: "); printstrln(str1);   //New: old string
My observation:
* strings are passed to C successfully!
* but the new value of string is not returned to XC.


srinie
XCore Addict
Posts: 158
Joined: Thu Mar 20, 2014 8:04 am

Post by srinie »

Hi,
It works in a similar C 'pass by reference' way.

Notes:
[*] keep an eye of array bound checks explicitly
[*] use string copy instead of direct string assignment
[*] similar to using "unsafe" in xC

Attached is the sample working code.
You do not have the required permissions to view the files attached to this post.
jerryXCORE
Experienced Member
Posts: 65
Joined: Tue Apr 30, 2013 10:41 pm

Post by jerryXCORE »

Hi, Srinie:

I tried and it works. Thanks a lot!