Dark Horse Programmer-07-Arrays, strings

Source: Internet
Author: User

First, the basic concept of the array 1. What is an array

An array, literally, is the meaning of a set of data, yes, an array is a set of data that is used to store

2. Features of the array

L can only hold one type of data, such as an array of type int, an array of type float

Two, the definition of the array 1. Defined

L declaring the type of an array

L DECLARE the number of elements in an array (how much storage space is required)

2. Format

Element type array name [number of elements];

3. Simple to use

L Simple initialization: int ages[5] = {19, 19, 20, 21, 25};

L elements have sequential points, each element has a unique subscript (index), starting with 0

l access to array elements: A[i]

4. Initialization

L Initialization mode

U inta[3] = {10, 9, 6};

U inta[3] = {10,9};

U inta[] = {11, 7, 6};

U inta[4] = {[1]=11,[0] = 7};

L Common Errors

U inta[];

U int[4]a;

U inta[b];

U a ={10, 11};

U a[4]= {10,9,8,5};

5. Storage of one-dimensional arrays

When you define an array, the system allocates a contiguous amount of storage space by array type and number to store the array elements, such as inta[3], which occupies a contiguous 6 bytes of storage (in a 16-bit compiler environment, an int type occupies 2 bytes). Note that the array name represents the address of the entire array, which is the starting address of the array.

U Note: Actually a is not a variable, it is a constant, it represents the address of the group. Put a in the variable column to facilitate understanding of the array structure.

The address of the U array A is ffc1,a[0] and the address of ffc1,a[1] is the address of ffc3,a[2] is ffc5. So a = = &a[0], that is, the address of the first element is the address of the entire array

6. Other uses

The element of the U-one-dimensional array is a one-way value pass as a function argument, as an argument to a simple variable of the same type, that is, the value of the array element is passed to the formal parameter, and the parameter change does not affect the argument

void Test (int b) {

b = 9;

}

int main ()

{

int a[3];

A[0] = 10;

printf ("a[0]:%d\n before function call", a[0]);

Test (a[0]); A[0] is the argument of the test function (actual argument)

printf ("a[0]:%d after function call", a[0]);

return 0;

}

* As you know, the array name represents the address of the entire array, and if the name of one-dimensional array is used as a function argument, the entire array is passed, that is, the shape parameter group and the real parameter group are identical and are the same arrays that are stored in the same storage space. When this parameter group is modified, the real parameter group is also modified. The number of elements in the shape parameter group can be omitted.

B is the parameter of the test function (formal parameter)

void Test (int b[]) {//can also write int b[3]

B[0] = 9;

}

int main ()

{

int a[3];

A[0] = 10;

printf ("a[0]:%d\n before function call", a[0]);

Test (a); A is the argument of the test function (actual argument)

printf ("a[0]:%d after function call", a[0]);

return 0;

}

Output Result:

Three or two-D array 1. What is a two-dimensional array

The L two-dimensional array is a special one-dimensional array: its elements are one-dimensional arrays. For example, int a[2][3] can be thought of as consisting of a one-dimensional array a[0] and a one-dimensional array a[1], which all two one-dimensional arrays contain 3 elements of type int

2. Storage

L Two-dimensional arrays are stored in rows, first the elements of the first row, and then the elements of the 2nd row. For example, the order in which int a[2][3] is stored is: a[0][0]→a[0][1]→a[0][2]→a[1][0]→a[1][1]→a[1][2]

3. Initialization

L Inta[3][4] = {1,2,3,4,5,6};

L Inta[3][4] = {{},{},{}};

L simple access to array elements

L Inta[][5] = {3,21,31,2,32,1};

L NOTE the error:

int a[3][4];

A[3] = {};

Four, string 1. What is a string

L Simple string "itcast"

L One ' i ' is a character

L A lot of characters are grouped together as strings.

2. Initialization of strings

L chara[] = "123"; and char a [] = {' 1 ', ' 2 ', ' 3 '}, the difference can be compared size

L "123" is actually made up of ' 1 ', ' 2 ', ' 3 ', ' + '

The output '%s ' of the string will not be output, which represents the end flag of the string.

V. Array of strings 1. Use Cases

* One-dimensional character array holds a string, such as a name char name[20]= "MJ"

* If you want to store multiple strings, such as the names of all students in a class, you need a two-dimensional character array, charnames[15][20] can hold 15 students ' names (assuming the name does not exceed 20 characters)

* If you want to store two classes of student names, then you can use a three-dimensional character array charnames[2][15][20]

2. Initialization

Char Names[2][10] = {{' J ', ' a ', ' y ', ' \ n '}, {' J ', ' I ', ' m ', '/'}};

Char Names2[2][10] = {{"Jay"}, {"Jim"}};

Char names3[2][10] = {"Jay", "Jim"};

Dark Horse Programmer-07-Arrays, strings

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.