How can I pass a Chapel string to a C function that requires (non-const) char*?

Viewed 64

Suppose I have a Chapel string

var s : string;

How would I send it to a function that expects a char*(since c_string assumes const char *)?

1 Answers

Here is an example that does this

extern {
  #include <stdio.h>
  static void f(char* argument) {
    printf("%s\n", argument);
  }
}

var s: string = "hello";
f(s.c_str():c_void_ptr:c_ptr(c_char));

Note the cast to c_void_ptr which is necessary before Chapel 1.19.

Related