Static array index in parameter Declaration: A good and little-known C Language Feature

Source: Internet
Author: User
C language creators must be keen to keep as few keywords as possible. Today we will show you another static keyword that can be used in c99.
You may have seen that the parameter Declaration of the array includes the length of the array:
void foo(int myArray[10]);

Such a function can still receive an integer pointer int *, but the length [10] can be used as a document for people who read the code, the information that the function expects to have an array of 10 integers.
In addition, you can add the static keyword [1] to the brackets:

void bar(int myArray[static 10]);

This will tell the compiler: The function will assume that the passed real parameters have at least 10 elements. (It is worth noting that this will exclude null !) This has two purposes:

1. the compiler can use this information when optimizing code [2]
2. When compiling a bar with the preceding Declaration, if the following three arguments [3] are passed in, the compiler can warn the Caller:
A. Pass null
bar(NULL);
warning: null passed to a callee which requires a non-null argument [-Wnonnull]    bar(NULL);    ^   ~~~~

B. Pass an array smaller than 10
int a[9];bar(a);
warning: array argument is too small; contains 9 elements, callee requires at least 10 [-Warray-bounds]    bar(a);    ^   ~

C. Pass an array (larger than 10)

int b[11];bar(b);

[no output]

This is useful when you are sure that the function can receive the length of the array, because it can provide document information for the person who reads the code and help the compiler capture errors.
[1]. you can also use the const keyword in brackets. this changes the pointer pointing to myarray to a pointer constant. [2]. it is not true that the compiler will actually optimize the code, because it is very unreliable for programmers to pay attention to the compilation warning information. [3]. compiler used: Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn).
Original article address: a nice, little known C feature: static array indices in parameter declarations read this article, but I checked the C language reference manual <C A Reference Manual>, when the function parameter declaration in c99 is found, it is indeed allowed to add a delimiter in the brackets of the array. <c a Reference Manual> chap 9, 9.3 formal parameter declaration, p296
"C99 extends statements for declarative parameters. A list of delimiters used to modify an array can appear in the upper [] brackets of the array annotator. array delimiters (type delimiters) const, volatile, and restrict treat arrays and pointer types similarly. that is to say, such parameter declaration:
T [ Qualifier-listE]
It is equivalent to the following statement:
T * Qualifier-listE
Example: The statements in c99 are given:
extern int f(int x[const 10]);extern int g(const y[10]);
In the f function, the parameter X is regarded as an int * const type (this is a pointer constant pointing to an integer type ), in the G function, the parameter Y is considered to have the const int * type (that is, a pointer to an integer constant ). note: A pointer constant pointing to an integer. the pointer value cannot be changed. It points to a variable integer, that is, the value of P cannot be changed, however, the value of * P can be changed. the subsequent constant pointer is a pointer to an integer constant, that is, P can be changed, but * P cannot be changed. in c99, the array separator static can also appear in the brackets of the array. its implementation of C (compiler) is an optimization prompt, asserted that the actual array parameter cannot be null and the type and length must be declared when entering the function body. without this qualifier, the NULL pointer can be passed in as an array as a real parameter, which makes it difficult for the compiler to know that it is safe. For example, when you enter the function, you must prefetch the content of an input array parameter. in addition, for the c99 form array parameter declaration in the prototype (cannot be a function definition), its length can be replaced by an asterisk (*), which means that the real parameter will be a variable length array. in the prototype declaration, any non-constant expression of the array size is equivalent to the asterisk. when defining a function, a very large number of expressions must be provided as the array size."
In fact, the purpose mentioned here is to allow the compiler to help detect more errors during compilation. however, it seems that most compilation does not support modifying the array parameters of function declaration. For example, the above example has no effect in GCC, and GCC will not give any prompts. the above suggestions only have the practical significance of static modification, that is, to help exclude null real parameters. all others have only academic research value. in fact, in actual use, the array parameters must be given with another parameter to convey the length of the array, as shown in the following figure:
void foo(int a[], int len);
This is advocated and widely used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.