C Language Learning Tutorials fourth-arrays (4)

Source: Internet
Author: User

Initialization of two-dimensional arrays
Two-dimensional array initialization is also assigned an initial value to each subscript variable in the type description. Two-dimensional arrays can be assigned in rows or rows, and can be assigned sequentially. For example, array a[5][3]:
1. Assignment by row segment can be written as static int a[5][3]={{80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85}};
2. Continuous assignment by row can be written as static int a[5][3]={80,75,92,61,65,71,59,63,70,85,87,90,76,77,85};
The results of these two initial assignments are exactly the same.
void Main ()
{
int i,j,s=0,l,v[3];
static int a[5][3]={{80,75,92},{61,65,71},{59,63,70},
{85,87,90},{76,77,85}};
for (i=0;i<3;i++)
{for (j=0;j<5;j++)
S=s+a[j][i];
V[I]=S/5;
s=0;
}
L= (V[0]+v[1]+v[2])/3;
printf ("Math:%d\nc languag:%d\ndbase:%d\n", v[0],v[1],v[2]);
printf ("total:%d\n", L);
}
The following instructions are also available for initializing a two-dimensional array:
1. You can only assign an initial value to part of the element, and the element that does not have initial values automatically takes 0.
Example: Static int a[3][3]={{1},{2},{3}}; is to assign a value to the first column of each row, and an unassigned element takes a value of 0. The values of each element after assignment are: 1 0 02 0 03 0 0
static int A [3][3]={{0,1},{0,0,2},{3}}; The value of the assigned element is 0 1 00 0 23 0 0
2. If an initial value is assigned to all elements, the length of the first dimension may not be given.
For example: static int a[3][3]={1,2,3,4,5,6,7,8,9}; Can be written as: static int a[][3]={1,2,3,4,5,6,7,8,9};
An array is a constructed type of data. A two-dimensional array can be thought of as a nested array of one-dimensional arrays. Each element of a one-dimensional array is an array, and a two-dimensional array is formed. Of course, the prerequisite is that each element type must be the same. Based on this analysis, a two-dimensional array can also be decomposed into multiple one-dimensional arrays. The C language allows this decomposition to have two-dimensional array a[3][4], which can be decomposed into three one-dimensional arrays whose array names are a[0],a[1],a[2]. You can use these three one-dimensional arrays without further explanation. These three one-dimensional arrays have 4 elements, for example: the elements of a one-dimensional array a[0] are a[0][0],a[0][1],a[0][2],a[0][3]. It must be emphasized that a[0],a[1],a[2] cannot be used as subscript variables, they are array names, not a simple subscript variable.

Character array

The array used to hold the character quantity is called a character array. The character array type description is the same as the numeric array described earlier. For example: Char c[10]; Because the character and integral types are universal, you can also define int c[10] But at this point each array element occupies 2 bytes of memory unit. A character array can also be a two-dimensional or multidimensional array, for example: Char c[5][10], or a two-dimensional character array. A character array also allows initialization of an assignment when the type description is used. For example: Static char c[10]={' C ', ', ' P ', ' R ', O ', G ', R ', ' A ', ' m '}; The value of each element after assignment is: array c c[0]c[1]c[2]c[3]c[4]c [5]c[6]c[7]c[8]c[9] c[ 9] is not assigned, and is automatically assigned a value of 0 by the system. The length description can also be omitted when an initial value is assigned to the whole element. For example: Static char c[]={' C ', ', ' P ', ' r ', ' O ', ' g ', ' r ', ' A ', ' m ', when the length of the C array is automatically set to 9.
Main ()
{
int i,j;
Char a[][5]={{' b ', ' A ', ' s ', ' I ', ' C ',},{' d ', ' B ', ' A ', ' s ', ' E '}};
for (i=0;i<=1;i++)
{
for (j=0;j<=4;j++)
printf ("%c", A[i][j]);
printf ("\ n");
}
}
The two-dimensional character array of this example is given an initial value as all elements are initialized, so the length of the one-dimensional subscript can not be explained. The string does not have a special string variable in the C language, and it usually holds a string with a character array. When describing string constants in the 2.1.4 section, it has been stated that the string always ends with ' I ' as a string. So when a string is stored in an array, the Terminator is also stored in an array and is used as a flag for whether the string ends. With the ' I ' flag, it is no longer necessary to use the length of the character array to determine the length of the string.
The C language allows you to initialize the array with a string of values. For example:
static char c[]={' C ', ', ' P ', ' r ', ' O ', ' g ', ' r ', ' A ', ' m '}; Can be written as:
static char c[]={"C Program"}; or remove {} write as:
Sratic Char c[]= "C program";

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.