linux C arrays and pointers

array:

Several variables with the same type are organized in an ordered form, and these collections of data elements of the same type become arrays.

Array is also a composite data type

The storage space of the 4 elements of the array count is adjacent. The members of an array can be primitive data types or composite data types. Elements of an array are accessed by subscripting.

Array subscripts can be expressions, but expressions must be integers.

Although arrays and structures have similarities, they also have obvious differences: arrays cannot be assigned or initialized to each other, and since they cannot be assigned to each other, array types cannot be used as parameters or return values ​​of functions.

One-dimensional array:

Element type in the array array name [number of elements in the array] = {element 0, element 1, ....};

E.g:int a[10]={1,2,3,4,5,6,7,8,9,0};   (int a[10];) 

Two-dimensional array:
The general form of a two-dimensional array definition is:
type specifier array name [constant expression 1] [const expression 2]
where constant expression 1 represents the length of the first dimension subscript, and constant expression 2 represents the length of the second dimension subscript .

int a[3][2]={1,2,3,4,5};

If less than 3 characters are automatically filled with zeros, the preceding or middle omission is not allowed. If you want to omit, please add zeros manually.
Array a has 3 elements, a[0], a[1] and a[2]. Each element is also an array, such as a[0] is an array, it has 3 elements a[0][0], a[0][1] These 2 element types are int, respectively 1, 2, the same The two elements of the rational array a[1] are 3 and 4, and the two elements of the array a[2] are 5 and 0.
All elements in an array are of the same type, and there is no two data types in a unified array.
The initialization of a two-dimensional array is also to assign an initial value to each subscript variable when the type is specified. A two-dimensional array can be assigned row by row, or row by row.
For example, for the array a[5][3]:
  • The line-by-line assignment can be written as:
    int a[5][3]={ {20,21,22}, {23,24,25}, {26,27,28}, {29,30,31}, {32,33,34} };
  • Line-wise continuous assignment can be written as:
    int a[5][3]={ 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34};
The results of these two initial assignments are exactly the same.
3) An array is a constructed type of data. A two-dimensional array can be viewed as a nesting of one-dimensional arrays. Assuming that each element of a one-dimensional array is another array, a two-dimensional array is formed. Of course, the premise is that each element type must be the same. According to such analysis, a two-dimensional array can also be decomposed into multiple one-dimensional arrays. The C language allows this decomposition.
Character array:
Arrays used to store character quantities are called character arrays.

pointer:

The so-called pointer is a computer language variable used to indicate a memory address or a register in the central processing unit. Pointers generally appear in languages ​​that are closer to machine language. Pointers generally point to a function or a variable. When using a pointer, a program can directly use the memory address stored by the pointer, and can also use the variable stored in this address. or function value. In layman's terms, the address of the memory unit where a variable is located is stored in another memory unit, and the memory unit that saves the address is called a pointer.
& is the address operator, for example: int *pi=&i; means to define a pointer variable pi of type int and initialize it with the address of i.
Simple use of pointers:

Pointer variables as function parameters:
The parameters of the function can be not only integer, real, character, etc., but also pointer types.
Its role is to transfer the address of a variable to another function.

Use of pointers and arrays:

pointer to pointer:
A pointer can point to a basic type or a composite type, so it can also point to another pointer variable called a pointer to a pointer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325552640&siteId=291194637