Part C array (one-dimensional array - at)

Then the article continues to say something about the array in C.

 

6. array name as the function parameters

When an array name is passed as an argument to a function of what would happen?

Value array name is a pointer to the first element of the array, it is readily apparent at this time passed to the function is a copy of the pointer. If the next function performed subscript references actually perform an indirect pointer to the access operation, and such indirect access, you can access and modify the function calling program array elements.

Now be explained on the upper surface C of the transmission parameters of contradictions. Said before all the parameters passed to the function are carried out by value , but the behavior of an array name argument if it is passed through the pass-calling. Pass-call access to the data is achieved by passing a pointer to the desired element, and then perform an indirect access operation of the function pointers. The name of the array as a parameter is a pointer, subscript reference is indirect access to the actual execution.

So call by value array of behavior is reflected in what place? Passed to the function is a copy of the parameters (start position pointer points to an array of copies), the function can freely operate its pointer parameter, without worrying about the corresponding modified pointer as an argument.

So, there is no contradiction here. All parameters are passed by value. Of course, if you pass a pointer to a variable, the function pointer and indirect access operations performed, then the function can modify that variable. Although not seem obvious, but officially this situation occurs when an array as a parameter name. This parameter (pointer) is actually passed by value, the function pointer is obtained a copy, which can be modified, but the argument passed to the calling program is not affected.

The following is a simple function, these views for explaining.

void strcpy(char *buffer, char const *string){  /* 复制字符,直到遇到NULL字节 */  while((*buffer++ = *string++) != '\0');}

Which copies a string in the second parameter to the first parameter in the buffer pointed. Buffer calling program will be modified as a function of the parameters of the indirect access operations. However, regardless of function parameters (pointer) How modifications, will not modify the caller's pointer argument itself (but may modify the content it points).

Note that while * string ++ expression statement. It made the character string pointed to, and produce a side effect, is to modify the string, it points to the next character. In this manner modified shape parameter argument does not affect the calling program, as has been modified to pass only a share of the copy function.

7. Declare array parameter

Here's an interesting question. If you want to pass an array name parameter to the function, the correct function parameter should be like? It should be declared as a pointer or an array?

As you can see, the actual function call is passed a pointer, the function parameter is actually a pointer, but easier to use for novices some compilers also accepts an array of function parameter. Thus, the following two types of functions are equal:

int strlen(char *string);

int strlen(char string[]);

This equality implies pointer and array name is actually equal, but it do not be fooled! These two statements do the same, but only in the context of the current environment. If there is elsewhere, it may be completely different, so as previously discussed. But for the array parameter, you can use any form of statement.

You can use any form of statement, but which "more accurate" mean? The answer is a pointer. Because the argument is actually a pointer instead of an array. Similarly, the value sizeof (string) is a pointer to a character length rather than length of the array.

You should now be clear why an array shape function prototype parameters do not need to specify the number of elements in it, because the function does not allocate memory for the array parameters. Parameter is just a pointer to the memory of good has been assigned elsewhere in space. This fact explains why array parameter array may be matched with any of its actual length ---- just passed a pointer to the first element of the array. On the other hand, this implementation can not know that the function of the length of the array. If you need to know the length of the array, it must be passed as a parameter to the function explicit.

8. initialization

Like scalar variables can be initialized as in their statement, the array can do the same. The only difference is to initialize the array requires a series of values. These values ​​are located between a pair of braces, each value separated by commas. As shown in the example below:

int vector[5] = {10, 20, 30, 40, 50};

Initializes the value assigned to the given list one by one each element of the array, vector [0] value is obtained 10, vector [1] value is obtained 20, and so on.

Static and automatic initialization

Way array initialization is similar to scalar variables are initialized ---- that is, depending on their storage type. Stored in the memory array of static initialization only this, that is, before the program started. Program does not need to perform these command values ​​in the right place, they start there. This process is done by the linker, which is initialized with the array elements in the file containing the executable program an appropriate value. If the array is not initialized, the initial values ​​of the array elements will be automatically set to 0. When this file is loaded into memory and executed, and program instructions as array values ​​after initialization are also loaded into memory. Therefore, when the program is executed, a static array has been initialized.

However, for automatic variables, the initialization process is not so simple, since the automatic variable located runtime stack, each entry thereof execution flow when the code block, each such variable may not be located in the same memory location . Before the program began, the compiler is no way to initialize these locations. So by default automatic variable is uninitialized. If the declaration of an automatic variable is given an initial value every time the execution flow automatically into the scope of the variable is declared, the variables will be an implicit assignment initialization. This implicit assignment needs and general assignments as time and space to perform. The problem is that the array initialization list may have a lot of value, which may produce many assignments. For those very large array, its initialization time may be considerable.

So here it is necessary to weigh. When initializing arrays local to a function (block), you should carefully consider, in the implementation of the program every time you enter the stream function (or block), each time to re-initialize the array is not worth it. If the answer is no, you put the array is declared as static, so the array initialization performed only once before the program begins.

9. Incomplete initialization

In the following two statements declared it would happen?

int vector[5] = {1, 2, 3, 4, 5, 6};

int vector[5] = {1, 2, 3, 4};

In both cases, the number of array elements and does not match the initialization value. The first one statement is wrong, we have no way to six integer value loaded into five integer variables. However, the second statement is legal, it provides an initial value for the first four elements of the array, the last element is initialized to zero.

So, can we omit the list of those values ​​in the middle of it?

int vector[5] = {1, 5};

The compiler only knows the initial value is not enough, but it can not know the value of those missing. Therefore, only the last few initial values ​​omitted.

10. The array length is automatically calculated

Here is another example of useful tips

int vector[] = {1, 2, 3, 4, 5};

If the statement does not give the length of the array, the compiler to put the array length is set to be just able to accommodate the length of all the initial values. If the initial list of values ​​often modified, this technique is particularly useful.

11. initialize an array of characters

According to current knowledge learned, you might think that a character array will be initialized following this form:

char Message [] = {H ',' e ',' l ',' l ',' o ', 0};

This method is of course possible, but except for very short strings, this method is really clumsy. Therefore, the language standard provides a quick method is used to initialize an array of characters:

char message[] = "Hello";

Although it looks like a string constant, not really . It's just another way of precedent initialization list.

If they look exactly the same, how do you distinguish between a string constant and rapid initialization list this notation it? They are distinguished according to the context in which they are located. When used to initialize an array of characters, it's a initialization list. In any other place, it represents a string constant.

Here is an example:

char message1[] = "Hello";

char *message2 = "Hello";

Both initialization looks like, but they have different meanings. The former initialize the elements of an array of characters, while the latter is a real string constant. The pointer variable is initialized to point to the storage location of the string constant, as shown below:


Published 60 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/BadAyase/article/details/101369236