Getting started with one-dimensional array in C Language

Source: Internet
Author: User

This article briefly introduces the entry-level C language getting started tutorial. Next I will introduce the use of one-dimensional arrays in C language, including one-dimensional arrays, array introduction, and array traversal, array initialization.

1. One-dimensional array

1. Define a one-dimensional array:
(1) Format: storage class type identifier array name identifier [constant expression]
(2) Example: static int a [10]
(3) Note: when defining an array, the element of the group must be a constant and cannot be a variable. For example, int n = 3; int a [n] is incorrect.

2. array reference:

(1) reference format: array name [subscript] For example, a [2]
(2) Note that the subscript of the array starts from 0. For example, a [3] indicates that three elements are a [0], a [1], and a [2]. if you use a [3] to access the third element, it is incorrect.
(3) array traversal: If you want to access all elements of the array at a time, you can only use the traversal form, as shown below:

The Code is as follows: Copy code

For (int I = 0; I <5; I ++ ){
Printf ("d % n", a [I]);
}

(4) How to input an array?
Note that you cannot enter an array as a whole. If you want scanf ("% d,", a); where a is an array, you can only enter a [0]. because the array name represents the first address of the array, it is equivalent to scanf ("% d,", & a [0. If you want to input all the data, you need to use the following method:
For example:

The Code is as follows: Copy code
For (int I = 0; I <5; I ++ ){
Scanf ("d % n", & a [I]);
}

3. Storage Structure of Arrays:
(1) There is an integer array a with four elements in it. I know that the memory address of the first element is 1000. How can I find the memory address of the third element?
Solution: the C language opens up continuous storage units for arrays. Each element occupies the same number of bytes. Because an integer generally occupies 2 bytes, therefore, each element occupies 2 bytes of memory space. The actual address of the array is 1000, so the address of the second element is 1002, the third element is 1004, and the fourth element is 1006.
Based on this principle, a formula can be obtained:
Element address of the array = starting address of the array + element subscript x sizeof (array type)
 

Array initialization:

Solution: initialization Methods: int a [5] = {0, 1} or nt a [5] = {0, 1, 5, 4, 3}

Note: If the length of the element is not enough, the system will automatically add 0 to the subsequent element, just like the first a [5] = {0, 1 }, it is equivalent to a [5] = {0, 0, 0 }. Do not overdo it. This will cause overflow. For example, a [5] = {0, 1, 5 4, 3, 0, 5} is an incorrect behavior.

For example:

The Code is as follows: Copy code

Int a [10]; it indicates that the integer array a has 10 elements.

Float B [10], c [20]; it indicates that the real-type array B has 10 elements, the real-type array c, and 20 elements.

Char ch [20]; specifies the string array ch, which has 20 elements.

Sort by data (Bubble Sorting)

Compare two adjacent numbers sequentially starting from the first number. If the order is correct, no operation is performed. If the order is incorrect, the two numbers are switched. After the first (N-1) comparison, the maximum number is placed at the end, the second time you only need to consider the number of (N-1), and so on until the second (N-1) times after the comparison can be sorted.

The source program is as follows:

The Code is as follows: Copy code

# Define N 10

# Include "stdio. h"

Main ()

{

Int a [N], I, j, temp;

Printf ("please input % d numbersn", N );

For (I = 0; I <N; I ++)

Scanf ("% d", & a [I]);

For (I = 0; I <N-1; I ++)

For (j = 0; j <N-1-i; j ++)

{

If (a [j]> a [j + 1])

{

Temp = a [j];

A [j] = a [j + 1];

A [j + 1] = temp;

}

}

Printf ("the array after sort: n ");

For (I = 0; I <N; I ++)

Printf ("% 5d", a [I]);

}

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.