Note 06_ C language array Scope &

Scope

Any programming, the variable scope is defined in the program area is present over the variable region can not be accessed. C language, there are three places you can declare variables:

  1. In function block inside or local variables
  2. In all external functions global variables
  3. In the form of function parameters define the parameters of

Local variables

Variable declared inside a function or block is referred to as local variables. They can only be used or the function inside the block statement. Local variables outside the function is unknown.

Global Variables

Global variables are defined in the external function, usually at the top of the program. Global variables throughout the application life cycle are valid, within any function can access the global variables.

Global variables can be accessed any function. In other words, global variables throughout the program after the statement is available.

In the program, local variables and global variables can be the same name, but in function, if the same two names, use the value of local variables, global variables will not be used.

Formal parameters

Function parameters, formal parameters are treated as local variables in the function, if they are the same name as a global variable will prefer to use.

Global variables and local variables in memory difference :

  • Global variables stored in memory in the global memory area, occupied by the static memory cell;
  • Local variables stored in the stack, only the dynamically allocated memory cell is where the variables when the function is called.

More refer to: C / C ++ usage of static global variable and a local variable

Initialize local and global variables

When a local variable is defined, the system will not initialize it, you must initialize its own. Defining global variables, it initializes the system automatically, as follows:

type of data Initialization defaults
int 0
char '\0'
float 0
double 0
pointer NULL

Correctly initialize variables is a good programming practice, otherwise the program may sometimes produce unexpected results because an uninitialized variable causes some garbage value in the memory location already available.

Array

C has an array data structure that can store a sequence of elements of the same type of fixed-size set. An array is used to store a series of data, but it is often considered to be a series of variables of the same type. Specific elements in the array can be accessed by index.

All arrays are composed of contiguous memory locations. The lowest address corresponds to the first element, the highest address corresponding to the last element.

Declare an array

To declare an array in C, the type and the number of elements specified element needs, as shown below:

type arrayName [ arraySize ];

This is called one-dimensional array. arraySize must be an integer constant greater than zero, type can be any valid C data types. For example, to declare an array of type elements 10 comprise a double Balance , the following declaration:

double balance[10];

Now balance is available array 10 can accommodate double the number of type.

Array initialization

In C, you can initialize the array one by one, you can also use an initial statement, as follows:

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values ​​between the braces {} is not larger than the number of elements in an array we specify a statement in square brackets [].

The number of elements in the initialization If you omit the size of the array, the array size was. Therefore, if:

double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

You will create an array, the array it created in the previous example is exactly the same. Here is an example of an element array assignment is:

balance[4] = 50.0;

The above statement is the value of the array elements in the fifth Fu 50.0. All are based on an array index 0 as the first element of which, also called based index, a last index of the array is the total size of the array minus one.

Access array elements

Array elements can be accessed through an array name indexed. Index of the element is placed in square brackets, followed behind an array of names.

C Array Detailed

In C, arrays are very important, we need to know more details about the array. Here are some important concepts related to an array of C programmers must be clear:

concept description
Multidimensional Arrays C supports multidimensional arrays. The simplest form of a multidimensional array is a two-dimensional array.
Pass an array to a function You can not bring the array by specifying the name of the index to pass a pointer to an array of pointers to functions.
Return an array from a function C allowed to return from the function array.
Pointer to an array You can not bring the array by specifying the name of the index to generate a pointer to the first element in the array pointed.

Multidimensional Arrays

The general form of a multidimensional array declaration is as follows:

type name[size1][size2]...[sizeN];

For example, the following statement creates a three-dimensional array of integers 5104..:

int threedim[5][10][4];
Two-dimensional array

A two-dimensional array, in essence, is a list of a one-dimensional array. Declaring a column x row y 2D integer array form as follows:

type arrayName [ x ][ y ];

A two-dimensional array can be thought of as a table with rows x and y columns. The following is a two-dimensional array, rows 3 and 4 comprising:

int x[3][4];

img

Two-dimensional array initialization

Multi-dimensional array can be initialized to the specified values ​​in each row in parentheses. The following is an array with three rows and four columns.

int a[3][4] = {  
 {0, 1, 2, 3} ,   /*  初始化索引号为 0 的行 */
 {4, 5, 6, 7} ,   /*  初始化索引号为 1 的行 */
 {8, 9, 10, 11}   /*  初始化索引号为 2 的行 */
};

Nested inside parentheses is optional, the following initialization is equivalent to the above:

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Access two-dimensional array elements

A two-dimensional array elements are accessed by the subscript (ie, the array row index and column index). E.g:

int val = a[2][3];

The above statement gets an array of 4 rows of 3 elements.

The following program, we will use a nested loop to process two-dimensional array:

#include <stdio.h>
 
int main ()
{
   /* 一个带有 5 行 2 列的数组 */
   int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
   int i, j;
 
   /* 输出数组中每个元素的值 */
   for ( i = 0; i < 5; i++ )
   {
      for ( j = 0; j < 2; j++ )
      {
         printf("a[%d][%d] = %d\n", i,j, a[i][j] );
      }
   }
   return 0;
}

When the above code is compiled and executed, it produces the following results:

a[0][0] = 0
a[0][1] = 0
a[1][0] = 1
a[1][1] = 2
a[2][0] = 2
a[2][1] = 4
a[3][0] = 3
a[3][1] = 6
a[4][0] = 4
a[4][1] = 8

Pass an array to a function

If you want to pass a one-dimensional array in the function as a parameter, you must be in the following three ways to declare a function formal parameters, the results of these three statements is the same way, because each method will tell the compiler is to receive a int pointer. Similarly, you can also pass a multi-dimensional array as a formal parameter.

Mode 1

Formal parameter is a pointer:

void myFunction(int *param)
{

}
Mode 2

Formal parameter is a defined array size:

void myFunction(int param[10])
{

}
Mode 3

In the form of an undefined parameter is the size of the array:

void myFunction(int param[])
{

}

To function, the length of the array is irrelevant, since the parameter C will not form boundary checking is performed.

Return an array from a function

C language does not allow a return to full array as a parameter. However, you can return a pointer to an array by specifying the array name without an index. You can skip this chapter, and so after understanding the concept of C pointers, come to learn the content here.

If you want to return a one-dimensional array from a function, you must declare the function returns a pointer, as follows:

int * myFunction()
{

}

Further, C is not supported in the local variables outside the return address of the function, unless the local variables defined static variable.

The following function, it will generate a random number 10, and return them to use the array, which contains the note standard library function, as follows:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
/* 要生成和返回随机数的函数 */
int * getRandom( )
{
  static int  r[10];
  int i;
 
  /* 设置种子 */
  srand( (unsigned)time( NULL ) );
  for ( i = 0; i < 10; ++i)
  {
     r[i] = rand();
     printf( "r[%d] = %d\n", i, r[i]);
 
  }
 
  return r;
}
 
/* 要调用上面定义函数的主函数 */
int main ()
{
   /* 一个指向整数的指针 */
   int *p;
   int i;
 
   p = getRandom();
   for ( i = 0; i < 10; i++ )
   {
       printf( "*(p + %d) : %d\n", i, *(p + i));
   }
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

r[0] = 313959809
r[1] = 1759055877
r[2] = 1113101911
r[3] = 2133832223
r[4] = 2073354073
r[5] = 167288147
r[6] = 1827471542
r[7] = 834791014
r[8] = 1901409888
r[9] = 1990469526
*(p + 0) : 313959809
*(p + 1) : 1759055877
*(p + 2) : 1113101911
*(p + 3) : 2133832223
*(p + 4) : 2073354073
*(p + 5) : 167288147
*(p + 6) : 1827471542
*(p + 7) : 834791014
*(p + 8) : 1901409888
*(p + 9) : 1990469526

Pointer to an array

After the can first understand the concept of C pointers, come to learn in this chapter.

Array name is a constant pointer pointing to the first element of the array. Therefore, in the following statement:

double balance[50];

balance is a point & balance [0] pointer, i.e., address of the first element of the array of balance. Accordingly, the following program fragment p assigned to balance the first element of the address:

double *p;
double balance[10];

p = balance;

Use as a constant pointer array name is legal, and vice versa. Thus, * (balance + 4) is a balance [4] legitimate access to the data mode.

Once you have the address stored in the first element of p, you can use the p, (p + 1), * (p + 2) and so to access the array elements. The following example demonstrates these concepts discussed above to:

#include <stdio.h>
 
int main ()
{
   /* 带有 5 个元素的整型数组 */
   double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
   double *p;
   int i;
 
   p = balance;
 
   /* 输出数组中每个元素的值 */
   printf( "使用指针的数组值\n");
   for ( i = 0; i < 5; i++ )
   {
       printf("*(p + %d) : %f\n",  i, *(p + i) );
   }
 
   printf( "使用 balance 作为地址的数组值\n");
   for ( i = 0; i < 5; i++ )
   {
       printf("*(balance + %d) : %f\n",  i, *(balance + i) );
   }
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Using the array of pointers to the value
(P + 0): 1000.000000
(P +. 1): 2.000000
(P + 2): 3.400000
(P +. 3): 17.000000
(P +. 4): 50.000000
using the balance as an array address value
(balance + 0): 1000.000000
(Balance +. 1): 2.000000
(Balance + 2): 3.400000
(Balance +. 3): 17.000000
(Balance +. 4): 50.000000

In the example above, p is a pointer to a pointer of type double, which means that it can store a variable of type double. Once we have the address of p, * p will be given in p values corresponding address storage.


Reference from: https://www.runoob.com/cprogramming/c-tutorial.html

Guess you like

Origin www.cnblogs.com/rope/p/12039568.html