TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

C String Creation

67 点作者 fcambus超过 9 年前

5 条评论

jws超过 9 年前
Good news in the article, apparently asprintf is scheduled for the next POSIX standard. I wonder how one sees what is in the next POSIX standard, it would inform my choices of what I use. I&#x27;ll use something that works on all my platforms if it is headed to a standard, but usually not if it may head to oblivion.<p>Also open_memstream is POSIX 2008, now if it would just get into OS X…
评论 #11085242 未加载
评论 #11084375 未加载
jhallenworld超过 9 年前
Should we not be using wchar_t strings in modern C?<p><pre><code> int main(int argc, char *argv[]) { wchar_t buf[100]; wprintf(L&quot;Hello, world!\ntype something&gt;&quot;); if (fgetws(buf, 100, stdin)) wprintf(L&quot;You typed &#x27;%ls&#x27;\n&quot;, buf); if (argv[1]) { char *s = argv[1]; &#x2F;* Convert char string to wchar_t string *&#x2F; size_t len = mbsrtowcs(buf, &amp;s, 99, NULL); if (len != (size_t)-1) { buf[len] = 0; wprintf(L&quot;argv[1] is &#x27;%ls&#x27;\n&quot;, buf); } return 0; } </code></pre> It&#x27;s a pain, but the advantage is access to iswalpha() and friends.
评论 #11084361 未加载
评论 #11084200 未加载
评论 #11085830 未加载
hthh超过 9 年前
This is an interesting article, but the &quot;Portability&quot; comments could be a lot more useful: strndup and open_memstream are both &quot;POSIX 2008&quot;, but strndup can be used on OS X while open_memstream cannot.
评论 #11085862 未加载
评论 #11084393 未加载
评论 #11084834 未加载
rumcajz超过 9 年前
This is an advice to use C in a way that resembles higher level languages. But it you want to do so, why not simply use a higher level language?<p>The power of C -- which distinguishes it from most other languages -- is the ability to allocate almost everything statically.<p>In fact, the older I get the more I appreciate pre-Algol60 way to allocating stack frames statically.
评论 #11085808 未加载
kevin_thibedeau超过 9 年前
I see too much damage caused by strncpy() to ever recommend it for use. Code that is blissfully unaware of the non-guaranteed NUL or that repetitively does extra work to guarantee a NUL. Use strlcpy() if available or reimplement it.
评论 #11085856 未加载