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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

A nice, little known C feature: Static array indices in parameter declarations

252 点作者 ehamberg超过 12 年前

15 条评论

tptacek超过 12 年前
I am convinced there is some kind of long-term drinking game going on in the C standards committee to see how many different uses they can come up with for the word "static".
评论 #5239720 未加载
评论 #5239513 未加载
评论 #5241079 未加载
评论 #5239645 未加载
matthavener超过 12 年前
In C++, you can do this by passing the array by reference:<p><pre><code> void foo(int (&#38;foo)[10]) {} </code></pre> However, that is not "minimum of 10" but "exactly 10".<p>You can also get the size at compile time:<p><pre><code> template &#60;size_t N&#62; void foo(int (&#38;foo)[N]) { int newarry[N+2]; } </code></pre> I've used the above for something like this:<p><pre><code> template &#60;size_t N&#62; void safe_strcpy(char (&#38;buf)[N], const char *src) { strncpy(buf, src, N-1); buf[N-1] = 0; }</code></pre>
评论 #5239484 未加载
评论 #5239568 未加载
Nav_Panel超过 12 年前
Doesn't seem to work for me using GCC 4.7.2.<p>flags: -g -Wall -Wextra -std=c99 -pedantic<p>The following code compiles and runs (for me at least) with no errors. Tried with both stack and heap allocated arrays of various sizes.<p><pre><code> #include &#60;stdlib.h&#62; void foo(int array[static 10]) { (void) array; // suppress unused var compiler warning return; } int main () { int *x = calloc(10, sizeof(int)); int *y = calloc(9, sizeof(int)); int *z = calloc(11, sizeof(int)); foo(x); foo(y); foo(z); foo(NULL); int a[9]; int b[4]; int c[11]; foo(a); foo(b); foo(c); return 0; }</code></pre>
评论 #5239499 未加载
评论 #5240116 未加载
评论 #5246119 未加载
评论 #5239608 未加载
adamnemecek超过 12 年前
Can someone recommend a resource that talks about some interesting C stuff similar to this? I've looked at "Expert C Programming: Deep C Secrets" but found it a bit outdated. And the C standard is a bit dry :-).
评论 #5239786 未加载
评论 #5242016 未加载
matthiasv超过 12 年前
Unfortunately, gcc does not warn. I can pass a NULL pointer and arrays that are too small. But on the other hand, there is very little use, because arrays usually contain an arbitrary number of elements. This might be useful for matrices and vectors, but then I would rather wrap them in a struct anyway.
nathell超过 12 年前
I thought I knew C.
评论 #5239595 未加载
评论 #5239472 未加载
评论 #5241211 未加载
vvhn超过 12 年前
great, just what we need - yet another overload of the static keyword :-).
PySlice超过 12 年前
Why do many features added to C after the first standard have to be quirky and slightly incompatible (with C++, with existing implementations, etc.) like this?<p>Other examples are inline (different from C++, makes use of weird combinations with static and extern) and tgmath (compiler magic inaccessible to user-defined functions until C11).<p>They also seem to __barely__ improve the language without ever being "cool" or "interesting".<p>At least C++ has some standard data structures...<p>PS: Even Python has binary literals, while they were deemed "not useful enough" for C.
jmaygarden超过 12 年前
I've never seen this before:<p><pre><code> void bar(int myArray[static 10]) </code></pre> Is that standard, and if so, how long has it been?<p>I also ran across this construct in a quick search [1]:<p><pre><code> void bar(int myArray[const]) </code></pre> [1] <a href="http://stackoverflow.com/questions/3693429/c-parameter-array-declarators" rel="nofollow">http://stackoverflow.com/questions/3693429/c-parameter-array...</a>
评论 #5239327 未加载
cedricd超过 12 年前
Interesting, but the one comment I haven't seen yet on this thread is 'Why is this useful enough to be a compiler feature?'. I think this is especially relevant with a relatively slim language like C.<p>In the rare cases you need to do this sort of check why not just write a simple sizeof test?
评论 #5240533 未加载
评论 #5240582 未加载
评论 #5240545 未加载
apaprocki超过 12 年前
Using 'static' in this way <i>compiles</i> with both Oracle Studio 12 and IBM xlc 11, but neither exhibit the behavior that gcc is shown to have in the article. Passing in both NULL as well as an array of shorter length work just fine with no warning/error from the compiler.<p>So, YMMV.
yxhuvud超过 12 年前
[I'm away from a C compiler and can't test]:<p>What happen if you do<p>void bar(int fooArray[static 0]) {} ?<p>Is NULL allowed?
评论 #5239446 未加载
评论 #5240399 未加载
评论 #5239476 未加载
FlawedDesign超过 12 年前
Live test case: <a href="http://liveworkspace.org/code/2uk7hc$18" rel="nofollow">http://liveworkspace.org/code/2uk7hc$18</a>
galaktor超过 12 年前
a bit off-topic: Golang has a similar feature built into it's type system [1]<p>"The size of an array is part of its type. The types [10]int and [20]int are distinct."<p>[1] <a href="http://golang.org/doc/effective_go.html#arrays" rel="nofollow">http://golang.org/doc/effective_go.html#arrays</a>
huhsamovar超过 12 年前
This is legitimately awesome, and allows for cleaner code. Nice post!