pForth strings vs C strings

58 views
Skip to first unread message

Craig Lindley

unread,
Sep 18, 2018, 5:08:07 PM9/18/18
to pforthdev
I am trying to pass 2 strings from pForth to a primitive I am writing in C. I thought the way to do that would be something like:

c" String1" c" String2" myPrimitive

However myPrimitive is receiving two pointers to the same string. Here is my test.

c" String 1" c" String2" .  .

I get back the following

 1073674112  1073674112

Obviously the storage for parsing the string is being overwritten by the last c" string. Does pForth have a word/function that builds a real C null terminated string and leaves its address on the stack?


Phil Burk

unread,
Sep 19, 2018, 10:44:50 AM9/19/18
to pforthdev
The word C" has different behavior depending on whether it is inside or outside a word.
Enter:
   see c"
to see a decompiled definition.

  c" String 1" c" String2"
will place both string on the PAD.

  : FOO   c" String 1" c" String2" ;
will compile two strings into the dictionary at different locations.

> Does pForth have a word/function that builds a real C null terminated string and leaves its address on the stack?

I can't think of one. c" is defined in system.fth. You can define a similar word that does 
   0 c,
after the string as a NUL terminator. Then return the forth string address 1+

Phil Burk



--
You received this message because you are subscribed to the Google Groups "pforthdev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pforthdev+...@googlegroups.com.
To post to this group, send email to pfor...@googlegroups.com.
Visit this group at https://groups.google.com/group/pforthdev.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages