title: How can I pass a Zig String literal to C
body: <p>A Zig String literal is a <a href="https://ziglang.org/documentation/0.9.0/#String-Literals-and-Unicode-Code-Point-Literals" rel="nofollow noreferrer">single-item pointer to a null-terminated byte array</a>, which is perfect to be used as a C's <code>char *</code> String!</p>
  <p>However, when I try to use this simple C function from Zig:</p>
  <pre class="lang-c prettyprint-override"><code>int count_bytes(const char *str) {
    int count = 0;
    while(str[count]) {
      count++;
    }
    return count;
  }
  </code></pre>
  <p>Zig caller:</p>
  <pre><code>const std = @import(&quot;std&quot;);
  const c = @cImport({
      @cInclude(&quot;add.c&quot;);
  });
  const testing = std.testing;
  
  test &quot;should be able to count string length&quot; {
      try testing.expectEqual(0, c.count_bytes(&quot;&quot;));
  }
  </code></pre>
  <p>I get this error:</p>
  <pre><code>./src/main.zig:16:46: error: expected type '[*c]u8', found '*const [0:0]u8'
      try testing.expectEqual(0, c.count_bytes(&quot;&quot;));
                                               ^
  ./src/main.zig:16:46: note: cast discards const qualifier
      try testing.expectEqual(0, c.count_bytes(&quot;&quot;));
                                               ^
  </code></pre>
  <p><a href="https://zig.news/kristoff/what-s-a-string-literal-in-zig-31e9" rel="nofollow noreferrer">This article</a> explains about Zig String literals in a similar situation, but I was unable to use the trick to make the String non-const:</p>
  <pre><code>test &quot;should be able to count string length&quot; {
      var cstr = &quot;&quot;.*;
      try testing.expectEqual(0, c.count_bytes(&amp;cstr));
  }
  </code></pre>
  <p>Result is an even stranger error:</p>
  <pre><code>./src/main.zig:17:45: error: expected type 'comptime_int', found 'c_int'
      try testing.expectEqual(0, c.count_bytes(&amp;cstr));
                                              ^
  </code></pre>
  <p>I also tried casting the String to a <a href="https://ziglang.org/documentation/0.9.0/#C-Pointers" rel="nofollow noreferrer">C Pointer as shown in the Zig Docs</a>:</p>
  <pre><code>test &quot;should be able to count string length&quot; {
      var cstr: [*c]u8 = &amp;&quot;&quot;.*;
      try testing.expectEqual(0, c.count_bytes(cstr));
  }
  </code></pre>
  <p>Which also gives an error:</p>
  <pre><code>./src/main.zig:16:27: error: expected type '[*c]u8', found '*const [0:0]u8'
      var cstr: [*c]u8 = &amp;&quot;&quot;.*;
                            ^
  ./src/main.zig:16:27: note: cast discards const qualifier
      var cstr: [*c]u8 = &amp;&quot;&quot;.*;
                            ^
  </code></pre>
  <p>How can I do this?</p>
  <p>EDIT:</p>
  <p>I am getting suggestions that do not work with Zig 0.9, which is the latest stable release as I am writing this.</p>
  <p>Please try this out first if you think you know a solution... put the C file at <code>src-c/add.c</code>, and the Zig file at <code>src/main.zig</code>,
  then run this to try:</p>
  <pre><code>zig test src/main.zig -I src-c 
  </code></pre>