The conquest of the C pointer excerpt 5: Function parameters and empty subscript operators []

Source: Internet
Author: User

Declaration of formal parameters of a function

The C language can declare the formal parameters of a function as follows:

void func (int a[]) {//...}

For this way of writing, it seems like you want to pass an array to the function's arguments.

However, in C it is not possible to pass an array as a function parameter. However, in this case, you can only pass pointers to the initial elements of the array.

when declaring a function parameter , an array that is classified as a type can be interpreted as a pointer.

void func (int a[]) {}

Can be automatically interpreted as

void func (int *a) {}

At this point, even if you define the number of elements of the array, it will be ignored.

" Essentials "

int a[] and int *a have the same meaning only in the case of declaring a function parameter.

Here is an example of a slightly more complex formal parameter declaration:

void func (int a[][5])

The type of a is an array of int (number of elements 5) (the number of elements is unknown), so it can be interpreted as "a pointer to an int array (number of elements 5)". Therefore, the above statement would have meant:

void func (int (*a) [5])

" Essentials "

The formal parameters declared below have the same meaning.

int func (int *a); /* Notation 1 */
int func (int a[]); /* Notation 2 */
int func (int a[10]); /* Notation 3 */

Writing 2 and Writing 3 is the syntactic sugar of 1.

Second, about the empty subscript operator []

In the C language, the subscript operator [] can omit the number of elements without writing in the following cases.

For these cases, different compilers have their own special explanations, so they cannot be used as normal rules.

(1) Declaration of function parameters

As explained in the previous section, for function parameters, the outermost array is interpreted as a pointer, even if the number of elements defined is ignored.

(2), according to the initialization of the expression can determine the size of the array case

In the following case, the compiler can determine the number of elements based on the initialization expression, so the number of elements of the outermost array can be omitted.

int a[] = {1, 2, 3, 4, 5};char str[] = "abc";d ouble matrix[][2] = {{1, 0}, {0, 1}};char *color_name[] = {"Red", "GR Een "," blue "};char color_name[][6] = {" Red "," green "," Blue "};

Note: int a[]; Will error

When initializing an array of arrays, the compiler should be able to determine the number of elements if there is an initialization expression, seemingly even if it is not the outermost array. However, in the C language, it is permissible to initialize an array of the following, so it is not possible to simply confirm the number of elements outside the outermost array.

omitted form of int a[][3] = {/* int a[3][3] */{1, 2, 3}, {4, 5}, {6}};char str[][3] = {/* char str[3][5] omitted form */"hog E "," Hog "," Ho "};

It seems that you can consider having the compiler choose a maximum value, but the syntax of C does not.

If this is done to troubleshoot programmer programming errors, what does not make the above "ragged array" also an error? For this phenomenon, I have so far been baffled (is it only because of negligence?) )。

By the way, when initializing an array of such an untidy type, the element with no corresponding initialization expression is initialized to 0.

(3), using extern to declare global variables

A global variable is defined in one of several compilation units (. c files) and then declared from another code file through extern.

The number of elements is still required at the time of definition, but when using extern for declaration, the compiler can determine the actual array size at the time of the connection, so the number of elements of the outermost array can be omitted.

As stated earlier, the declaration of an array can be interpreted as a pointer only when the function parameter is declared.

When you declare a global variable as follows, the array and pointers are mixed together, and the compiler usually does not report any warnings or errors except that the program is not functioning properly. This requires attention (today's linker, and sometimes an error).

File_1.c ... In
int a[100];
File_2.c ... In
extern int *a;

Supplemental: Declarations and definitions

In the C language, "declaration" is called "definition" when defining the entity of a variable or function.

For example, the behavior of declaring a global variable as follows is "definition".

int A;

The following extern declaration means "make an object declared somewhere in the current place", so it is not "defined".

extern int A;

Similarly, the prototype of a function is "declaration", and the "definition" of a function refers to the part of the actual execution code that writes the function.

In the case of automatic variables, it is meaningless to distinguish between definitions and declarations, since declarations are inevitably accompanied by definitions.

The conquest of the C pointer excerpt 5: Function parameters and empty subscript operators []

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.