Perl & XS -- Modifying a char*

From: Shawn Zabel (zabelsr_at_alltel.net)
Date: 07/19/04


Date: Mon, 19 Jul 2004 17:35:02 -0400

I'm looking for suggestions on how to modify a char* argument passed into an
XS function. It's been two days now that a co-worker and I have been trying
to figure this out. We've seen examples on how to modify integers, but
nothing on strings or char pointers. The example below is meant only to
provide a crude example of what we are trying to accomplish. We are very
appreciative of any and all help provided of this issue.

***Example Perl Code***
my $p = "old value";

getLastPrompt($p);
  or
getLastPrompt(\$p);

$p now equals "new prompt"

***Example XS Code***
int getLastPrompt(prompt)
        char * prompt
    CODE:
        prompt = "new prompt";
        RETVAL = 1;
    OUTPUT:
        prompt
        RETVAL

- Shawn



Relevant Pages

  • Re: [PATCH] Make hash.h usable in the kernel
    ... break almost all callers. ... Callers want and need to pass char **, ... and then if they want to modify *end, to convert `end' to plain char *. ... leads to compiler warnings, which lead to even uglier things like the ...
    (freebsd-net)
  • problem with charset
    ... i've some problem to print to screen and save date in mysql if the ... char set: utf-8 unicode ... Modify (hope that you see 2 squares after abad it would ... Modify (hope that you see 2 squares after agog it would be ...
    (comp.lang.php)
  • =?UTF-8?B?UmU6IHdhcm5pbmc6IGRlcHJlY2F0ZWQgY29udmVyc2lvbiBmcm9tIHM=?= =?UTF-8?B?dHJpbmcgY29uc
    ... assigning a char * to a char *, ... certainly _can_ go wrong if you try to modify the pointed-to string, ... compiler that you can't modify what foo points to. ... to modify one line, then modify a dozen lines that depend on that, then the hundreds of lines that depend on those, and the single const has infected expressions all over your program. ...
    (comp.lang.c)
  • Re: impl a collection (watch out for big strings)
    ... Not the char[] array inside a String. ... modify it and no way to add one. ...
    (comp.lang.java.programmer)
  • Re: std::transform with std::use_facet >(std::locale()).toupper()
    ... (other than a compiler warning about assigning int to char - std::toupper()'s return value is int) ... take non-constant arguments (char* and LPTSTR*, ... It is explicitly defined as legal to modify the argv strings in the C standard, though the C++ standard doesn't say anything about whether you can modify them. ...
    (microsoft.public.vc.language)