Re:Learn C Programming with Open-Source Books
by DSMan195276 on Monday February 06, 2017 @01:32PM
(#13582440)
I think it's worth clarifying that arrays declared in parameter lists are simply just pointers. It's 100% syntactic sugar. So the `int a[n]` is simply treated as `int * a`, and the value inside of the `[]` is ignored completely. This leads to the weird situation that you declared it as `int a[n]`, but `sizeof(a)` just gives the size of a pointer, not the size of the array.
The only special case is `int a[static n]`, which does tell the compiler that `a` has at-least `n` entries. But `a` is still just a pointer in this instance, so `sizeof(a)` still gives the size of a pointer.
Personally, I'd recommend avoiding the syntax purely because of the `sizeof` issue, but its up to you.
Re:Learn C Programming with Open-Source Books
by kps on Monday February 06, 2017 @12:06PM
(#13581487)
VLAs were added in C99, but not universally adopted (particularly by embedded systems compilers, for obvious reasons), and then made optional in C11.
Re:Learn C Programming with Open-Source Books
by DSMan195276 on Monday February 06, 2017 @01:35PM
(#13582483)
You're completely right, but I think it's worth clarifying that VLA notation in parameter lists, and actual VLAs are two completely different things. The example he showed doesn't actually use a VLA, it just looks like it does.
Re:Learn C Programming with Open-Source Books
by pantalaimon on Monday February 06, 2017 @12:32PM
(#13581779)
I don't see an obvious reason - that's all information that's available on compile-time, there should be no additional run-time cost - or what are you implying?
EDIT: I didn't get a warning out of gcc
Compile: clang 3.8 warns about differing things: Still, it's a neat idea from a documentation point of view, and GNU-like compilers seem to accept it.A bit before is more explanation of on the arr[static ?] notation. I was aware of this and usually prefer to use (Clang/GCC) function annotations to indicate NULL-ness of pointers and such, but I see the advantage in some situations. I don't agree with everything in the document, but it contains a lot of cool things.