C-language function parameters and empty subscript operators []

Source: Internet
Author: User

Declaration of the formal parameters of a function

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

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

For this type of writing, no matter what it looks like to pass an array to the parameters of the function.
However, in C it is not possible to pass an array as a parameter to a function. In this case, however, you can only pass pointers to the initial elements of the array.

When a function parameter is declared, an array that is categorized 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 in the array, you will be ignored.
Points
int a[] and int *a have the same meaning only if a function parameter is declared.

The following 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 (element number 5), so it can be interpreted as a pointer to an array of int (element number 5). Therefore, the above statement originally meant:
void func (int (*a) [5])

Points

The formal parameters declared below have the same meaning.
int func (int *a); /* Writing 1/*
int func (int a[]); /* Writing 2/*
int func (int a[10]); /* Writing 3/*

Writing 2 and writing 3 are the grammatical sugars of 1.

second, on the empty subscript operator []

In the C language, the subscript operator [] can omit the number of elements without writing.
For these cases, different compilers have their own special interpretations, so they cannot be used as normal rules.

(1), Declaration of function parameters

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

(2), depending on the initialization of the expression can determine the size of the array

In the following case, the compiler can determine the number of elements based on the initialization expression, so you can omit the number of elements in the outermost array.

int a[] = {1, 2, 3, 4, 5};
Char str[] = "ABC";
Double Matrix[][2] = {1, 0}, {0, 1}};
Char *color_name[] = {
"Red",
"Green",
"Blue"
};
Char Color_name[][6] = {
"Red",
"Green",
"Blue"
};

Note: int a[]; Will complain

When initializing an array of arrays, the compiler should be able to determine the number of elements if there is an initialization expression that seems to be even the outermost array. However, in the C language, allowing such an irregular array to initialize, it is not possible to simply confirm the number of elements outside the outermost array.

int a[][3] = omitted form of {/* int a[3][3] * *
{1, 2, 3},
{4, 5},
{6}
};

Char str[][3] = omitted form of {/* char str[3][5] * *
"Hoge",
"Hog",
"Ho"
};

It seems possible to consider having the compiler choose a maximum value, but the syntax for C does not.
If this is done to troubleshoot programmers ' programming mistakes, what's wrong with the "ragged array" above? I have so far puzzled over this phenomenon (is it only because of negligence?) )。
Incidentally, elements that do not have a corresponding initialization expression are initialized to 0 when initializing such an untidy array above.

(3), using extern to declare a global variable

A global variable is defined in one of several compilation units (. c files) and is then declared from another code file through extern.
The number of elements is required at the time of definition, but when using extern declarations, the compiler can determine the actual size of the array at the time of the connection, so you can omit the number of elements in the outermost array.
As explained earlier, the declaration of an array can be interpreted as a pointer only when a function parameter is declared.
When a global variable declaration is made like this, the array and the pointer are mixed together, and the compiler usually does not report any warnings or errors except that the program does not function correctly. This needs to be noticed (now the linker, and sometimes the error).

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

additions: Declarations and Definitions

In C, "declaration" is referred to as "definition" when the entity of a variable or function is specified.
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 to be used at the current location," 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 necessarily accompanied by definitions

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.