C language learning-array study notes

Hello everyone, I am a Disheng who knows nothing. Today I learned the array of C language, let’s share it.

 

The best time to plant a big tree is ten years ago, followed by now.

Let's cheer and learn together~O(∩_∩)~

 

Please point out any errors, thank you!

Most of the variables in C language have the ability to store a single data (called scalar), and there is another kind of data called aggregate variable, which can store a group of data. There are two types of aggregation in C language, one is structure , The other is an array.

One-dimensional array

1. Definition: An array is a data structure containing multiple data values, and each data has the same data type (unified type).

2. Array declaration: The array declaration needs to declare its type and quantity. For example: int a[10]; This is to declare an array, which has 10 elements of type int.

The type of the array can be any type. Just modify the previous int declaration. The length of the array can be represented by any integer constant. After C99, it can also be represented by an integer variable (you can also use a macro and use a const definition, here is I won’t go into details http://c.biancheng.net/view/217.html, you can see this link (intrusion), because the length of the array may need to be adjusted when the program changes in the future, so the better way is to use variables to Indicates its length.

3. Array subscript:

In order to be able to access specific array elements, you can use square brackets to add an integer after writing the array name. This integer value is the subscript value and also the index value.

It should be noted that the subscript value of this array starts from 0!

For example, int a[10]; This array a is from a[0] to a[9]. The index of an array element of length n is from 0 to n-1 (in this matter, it always deviates by one bit)

(The traversal does not need to cross the boundary, beyond the subscript range, will cause undefined things to happen!)

4. Array tips-three idioms used in conjunction with for loops:

for (i = 0; i < N ;i++)
    a[i] = 0;
//以上为清零操作
for (i = 0; i < N; i++)
    scanf("%d",&a[i]);//注意scanf里面数组前也要用&
//以上是把数据存入数组a
for (i = 0; i < N; i++)
    sum += a[i];
//计算一些数字的和

5. Array initialization:

Like other variables, the element group must be given an initial value when it is declared.

There are many kinds of assignment methods, the following are the basics

(1). Use braces to enclose constant expressions: int a[5] = {1,2,3,4,5}

int a[5] = {1,2,3} is equivalent to int a[5] = {1,2,3,0,0} That is, if the initialization formula is shorter than the array, then the remaining elements in the prime group are 0 (But it is illegal for the initializer to be longer than the length of the array!)

Use the above features to int a[5] = {0}; note that {} cannot be empty!

(2). Specify the initialization formula

Consider the following example

int a[8] = {0,0,2,0,3,0,0,0}

Hope that a[2] = 2, a[4] = 3, we can int a [8] = {[2] = 0,[4] = 3}; This method is to use the indicator to assign int a [ 8] = {[ 2 ] = 0,[ 4 ] = 3}; The 2, 4 in the brackets are indicators, which must be integer constant expressions.

6. The sizeof operator of the array (very useful)

The operator sizeof can calculate the size of the element group (calculated as a number of bytes), you might think, this has something to do with me! Anyway, I thought so for the first time, let’s talk about its use

Look at a formula first 

int a[10] = {0};

for (i = 0; i < sizeof(a)/sizeof(a[0]);i++)
{a[i] = 0;


}

sizeof(a)/sizeof(a[0]) represents the length of the array, we often use this expression when we need the length of the array

However, some compilers may warn about sizeof(a)/sizeof(a[0]) because the type of variable i may be signed and the result of sizeof is an unsigned type.

Of course, if you want, you can (int) sizeof(a)/sizeof(a[0]) to force type conversion

7. Points to note when transferring parameters

When an array is used as a function parameter, another parameter must be used to pass in the x array size, because sizeof cannot be used to calculate the number of array elements when declaring a function parameter!

Declare that the function is parameter a[] There can be nothing in the square brackets!

8. Multidimensional array

1. Definition: The array can have any dimension, such as a two-dimensional array (matrix in mathematics) int a[5][8] declared below; the array has 5 rows and 8 columns, when such an array accesses its elements They are all written in the form of i[m][n].

2. Initialization of multi-dimensional array:

#include<stdio.h>

int main()
{
	int a[5][6] = 
	{
   
   {1,2,3,4,5,6},
	 {2,0,0,0,0,0},
	 {3,0,0,0,0,0},
	 {4,0,0,0,0,0},
	 {5,0,0,0,0,0}};
	 
	 printf("%d",a[0][2]);
	
}

The above method is to initialize the multi-dimensional array. There are many methods. You can also put a lot of data in one {} and write a large long line, because when saving, each line will be stored directly from the next line after it is full, but This is not intuitive (in fact, the two-dimensional array is essentially arranged into a large row of elements)

int a[4][5] = { {0,1,2,3,4},{10,11,12,13,14},{20,21,22,23,24},{30,31,32,33,34}};

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_51182221/article/details/114966266