10-c language Arrays, strings

Source: Internet
Author: User

First, the basic concept of the array

A variable of type int can save a person's age, if you want to save the whole class age?

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

The data stored in the L is called "element"

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];

For example: int ages[3];

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 int a[3] = {10, 9, 6};

u int a[3] = {10,9};

u int a[] = {11, 7, 6};

u int a[4] = {[1]=11,[0] = 7};

L Common Errors

u int a[];

U int[4] A;

u int a[b];

U a = {10, 11};

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

5. Memory Analysis

L size of the array storage space

L Partition of storage space (memory allocation is from high address to low address, but an array interior element is from low to high)

L array name function, view element address

L array out of bounds attention

6. Other uses

L Array and function parameters

U array elements as function parameters

U array as function parameter (sizeof note)

L Traversing array elements

U Two Way traversal (while loop and for loop)

U traverse element value and element address

Use of the U-character array

7. Exercises

1> prompts for 5 students ' scores, calculates the average score, the highest score, the lowest score, and then outputs

2> Design a function: int arrayMax (int a[], int count) to find the maximum value of an array element

3> Design a function: int arraysum (int a[], int n), and the number of the first n of a one-dimensional array

4> design a function that stores elements in a one-dimensional integer array in reverse order. For example, it would have been 1,3,4,2, reverse storage has become: 2,4,3,1

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

L An array can represent the age of a class, if you want to express a lot of classes?

L What is a two-dimensional array? int ages[3][10]; Three classes, 10 per class.

L equivalent to 3 rows and 10 columns

L equivalent of 3 one-dimensional arrays

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 Storage Size

L Storage structure and order

L Storage Address issues

3. Initialization

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

l int A[3][4] = {{},{},{}};

L simple access to array elements

l int A[][5] = {3,21,31,2,32,1};

L NOTE the error:

int a[3][4];

A[3] = {};

4. Traverse

L Traverse all the elements

L Traverse Address

L Use occasions: Gobang, Tetris

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 char a[] = "123"; and char a [] = {' 1 ', ' 2 ', ' 3 '}, the difference can be compared size

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

• Storage distribution of "123"

L The output of the string "%s", ' \ n ' is not output

3. The role of the

L output char a[] = {' O ', ' K '};

L got "MJ" in front of Char a[].

L Output "MJ"

L re-output a

L Char a[] = {' I ', ' t ', ' + ', ' C '};

4. Commonly used string processing functions

L strlen (note Chinese)

5. Exercises

Write a function char_contains (char Str[],char c), if the string str contains the character C will return the value 1, otherwise return the value 0

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, char names[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 Char names[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"};

10-c language 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.