Basic concepts of arrays
what is an array: An array is:An array is a collection of elements of the same typetype specifier array name [constant expression];where the type descriptor is either a basic data type or a construction data type. The array name is a user-defined array identifier. The constant expression in square brackets represents the number of data elements, also known as the length of the array. For example:int a[10];/* Description int array A, with 10 elements * /float b[10], c[20];/* Description of the real array B, there are 10 elements, a real array C, there are 20 elements * /Char ch[20];/* Description character array ch, with 20 elements * /The following five points should be noted in the array:the type of the array is actually the value type of the exponential group element. For the same array, all of its elements have the same data type. the writing rules for array names should conform to the writing requirements of identifiers. the array name cannot be the same as other variable names. The constant expression in square brackets represents the number of array elements, such as a[5], which indicates that array a has 5 elements. But its subscript is calculated starting from 0. So 5 elements were a[0], a[1], a[2], a[3], a[4]. You cannot represent the number of elements in a variable in square brackets, but can be a symbol constant or a constant expression
Application of Arrays
1, one-dimensional array reference
An array element is the basic unit that makes up an array. An array element is also a variable whose identity method is followed by a subscript after the group name. the subscript indicates the ordinal number of the element in the array. The general form of an array element is:
Array name [subscript]
Where subscript can only be an integer constant or an integral type expression. In the case of decimals, c compilation is automatically rounded. For example:
A[5]
A[I+J]
a[i++]
are valid array elements.
array elements are also commonly referred to as subscript variables. An array must be defined before the subscript variable can be used. You can use subscript variables only one at a time in the C language, not the entire array at once. For example, an array that outputs 10 elements must use a looping statement to output each subscript variable individually:For (i=0; i<10; i++)printf ("%d", A[i]);instead of outputting the entire array with a single statement. Therefore, the following notation is incorrect:printf ("%d", a);
2. Initialization of one-dimensional arrays:
the method of assigning values to an array can also be initialized with assignment and dynamic assignment, in addition to assigning values to arrays of elements in an assignment statement.
Array initialization assignment refers to assigning an initial value to an array element when the array is defined. Array initialization is done during the compile phase. This will reduce running time and increase efficiency. The general form of the initialization assignment is:
Type specifier array name [constant expression] = {value, value ... Value};
Each data value in {} is the initial value of each element, with a comma interval between the values. For example:
- int a[10]={0,1,2,3,4,5,6,7,8,9};
equivalent
a[0]=0; A[1]=1. a[9]=9;
the initialization assignment of the C language array also has the following provisions: 1) You can assign an initial value to only a subset of elements. When the number of values in the {} is less than the number of elements, only the previous elements are assigned values. For example:int a[10]={0,1,2,3,4};indicates that only a[0]~a[4]5 elements are assigned, and then 5 elements are automatically assigned a value of 0. 2) You can assign values to elements only, not to the whole array. For example, all 10 elements are assigned a value of 1 and can only be written as:int a[10]={1,1,1,1,1,1,1,1,1,1};and cannot be written as:int a[10]=1;3) If the entire element is assigned a value, then in the array description, you can not give the number of elements of the group. For example:int a[5]={1,2,3,4,5};can be written as:int a[]={1,2,3,4,5};
multidimensional Arrays (two-dimensional arrays)
here we mainly discuss two-dimensional arrays:the general form of a two-dimensional array definition is as follows:
Type identifier array name "constant expression 1" constant expression 2 ";
For example:
int a[2][3];
float B[3][10];
There are two types of initialization of a two-dimensional array:
(1) Branch initialization, such as:
static int a[2][3]={{1,2,3,},{4,5,6}};
(2) Unified initialization, such as:static int a[2][3]={1,2,3,4,5,6};
Basic concepts of pointers
In the computer, all of the data is stored in memory, the memory of a byte is generally referred to as a memory unit, the different data types occupy the same number of memory units, such as int occupies 4 bytes, char occupies 1 bytes. In order to access these memory cells correctly, each memory unit must be numbered. The number of each memory unit is unique and can be accurately identified by the number.
The number of memory units is called address, also known as Pointer (Pointer).
The contents of a memory unit's pointer and memory unit are two different concepts. You can use a popular example to illustrate the relationship between them. When we use the bank card to withdraw money from the ATM, the system will find the account information according to our card number, including the deposit and withdrawal record, balance, etc., the information is correct, the balance is sufficient to allow us to withdraw funds. Here, the card number is the account information of the pointer, deposit and withdrawal records, balance, etc. is the contents of the account information. For a memory unit, the address (number) of the cell is the pointer, and the data that is stored is the contents of the cell.
In the C language, a variable is allowed to hold a pointer, which is called a pointer variable . Therefore, the value of a pointer variable is the address of a memory cell or a pointer to a memory unit.
With the character variable C, the content is ' K ' (ASCII code is the decimal number), C occupies the 0x11a number (address is usually expressed in 16 in number). With the pointer variable p, the content is 0x11a, which we call the P-pointing variable C, or P is a pointer to the variable C.
Strictly speaking, a pointer is an address and is a constant. A pointer variable can be given a different pointer value, which is a variable. But the pointer variable is often referred to as a pointer. To avoid confusion, this tutorial conventions: "Pointers" means addresses, constants, and "pointer variables" are variables that take values as addresses. The purpose of defining pointers is to access the internal deposit cells via pointers.
Since the value of the pointer variable is an address, the address can be not only the address of the variable, but also the address of other data structures. What is the point of holding an array or the first address of a function in a pointer variable?
Because arrays or functions are stored continuously. The array or function is found by accessing the pointer variable to get the first address of the array or function. In this way, wherever an array is present, the function can be represented by a pointer variable, provided that the pointer variable is given the first address of the array or function. Doing so will make the concept of the program very clear and the procedure itself concise and efficient.
in the C language, a data type or structure often occupies a contiguous set of memory units. The concept of "address" does not describe a data type or structure well, while a "pointer" is actually an address, but it is the first address of a data structure, which is "pointing" to a data structure, so that the concept is clearer and more explicit. This is also an important reason to introduce the concept of "pointer".
application of first-level pointers
The so-called first-level pointer can be said to store variable address pointer variable, such as int a = 1; int *p = &a;
(1) & Take address operator
in the previous scanf statement, there were & address characters, such as
int A; An integer variable is defined, and the system assigns a memory address to the integer variable.
scanf ("%d", &a); Put the data that is being read into this memory address.
printf ("a=%d", a); Output the value of this variable (the value of the memory address).
If you use pointers instead, the program can change to this:
int a,*p=a;
scanf ("%d", p);
printf ("a=%d", a);
(2) * FETCH (pointer) content operator
we can also use certain actions to extract the contents of the saved address in the pointer.
such as: int a,*p=a;
scanf ("%d", p);
printf ("a=%d", *p);
Note that the first line int *p; * is just to show that the P variable is a pointer variable. the *p of the third row is the output of the value stored in the memory cell address stored by the pointer variable p. Example: Input 2, output a=2It is important to note that after we have defined a pointer variable, we will in principle be associated with normal variables such as:
#include "stdio.h" void Main () { int *p; int a=5; printf ("%d%d\n", p,a); printf ("%d%d", *p,&a);}
It is important to note that although the P pointer variable is defined, it does not assign p=&a such an assignment, so only the output
Output: 1245068 5
4394640 1245048
The first line of the first number system is assigned to an address (integer) of P, the second is the value of a variable;
The second line is the value of the in-memory address stored in the P pointer, which is the random value given by the system, and the next one is the address of the system to the A variable.
#include "stdio.h" void Main () { int *p; int a=5; p=&a; printf ("%d%d\n", p,a);
Output: 1245048 5
5 1245048
The first number of the first line is the memory address that the pointer variable holds, that is, the memory address that the system assigns to the A variable, and the second number is the value of the A variable.
The first number in the second row is the value of the memory address that the P variable holds, which is the value of a. The last number output is the memory address assigned to the A variable by the system.namely at this time: P==&a *p==a All is established.
Application of advanced pointers (level two pointers)
The so-called two-level pointer can be said to store pointer variable address of the pointer variable, such as int *q = &p;The application of level two pointers, illustrated by an example, is a C language lookup function, a function prototype: void Find1 (char array[], char search, char *pa)The array in this function parameter is a string that ends with a value of $, requiring the same character to be found in the string array as given by the parameter search. If found, returns the address of the first encountered character in the array string by the third parameter (PA). If not found, the PA is null.
according to test instructions, the implementation code is as follows.
void Find1 (char [] array, char search, char *pa) {for (int i = 0; * (array + i)! = ' + '; ++i) { if (* (ARRA Y + i) = = search) { PA = = array + i; The PA gets the address of an element break;}}}
Does this function implement the required functionality?
Call this function below to try
int main (int argc, char *argv[]) { char str[] = "Abcdefdfefde"; String to be found char a = ' d '; Sets the character to find char *p = NULL; First null find1 (str, A, p); Call the function to implement the find Operation if (NULL = = p) { cout << "not found!" << Endl; } else { cout << "Found it!" << Endl; } return 0; }
Run result is not found! why?!
Analysis: First look at the function definition:
void Find1 (char [] array, char search, char *pa)
look at the use of the tune again:
find1 (str, A, p);
think carefully about what happens when the parameter is combined: the array is named STR, and search gets the value of a, and the PA Gets the value of P (which is null, not the address of P itself)! But the argument p does not get the value (the address of an element) returned by the parameter Pa. Although pointers are used, they are not transmitted, and when the arguments are pointers, they are only values--passing people's addresses and not coming back. At this point, we can use a level two pointer .
FIX:
void Find2 (char [] array, char search, char **ppa) {for (int i = 0; * (array + i)! = ' 0 '; ++i) { if (* (arr ay + i) = = search) { *ppa = array + i; break; } } if (' + ' = = * (array + i)) { *ppa = NULL: } }
The main function is changed at the following call:
Find2 (str, A, &p); Calling functions to implement operations
The PPA is a pointer to the pointer p. The modification to *ppa is the modification of the pointer p. Note: Do not copy the pointer's usage to the pointer pointer's head! such as:Char *pa and const char *PA are type-compatible;but Char **pa and const char **PA are type incompatible! It seems that we have found the application of level two pointers, which is the pointer variable that holds the pointer variable address.
the relationship between an array and a pointer
11-D Arrays and pointers:
we know that when an array is defined, the system allocates a contiguous memory address, such as:
int A[5] will be assigned a contiguous space to represent the address of a[0] a[1] a[2] a[3] a[4] a[5].
#include "stdio.h" void Main () { int a[5],i=0; for (i=0;i<5;i++) printf ("%d", &a[i]);
output:
1245036 1245040 1245044) 1245048 1245052
1245036
As you can see, the for loop once outputs the address of the items in the A array, which is continuous. This compilation environment supports an int variable that occupies four bytes.
The last output is the array name,
because the array name represents the first address of the array, that is,
a==&a[0] is established.
At this point, if the address is represented by a pointer, the first address of the array can be saved directly. As
#include "stdio.h" void Main () { int a[5]={11,12,13,14,15},*p,i; for (i=0;i<5;i++) printf ("%d", A[i]); printf ("\ n"); p=a;//or written p=&a[0]; printf ("%d\n", *p); printf ("%d\n", * (p+2)); p=p+1;
22-D arrays and pointersI believe that the ambiguous lingering between pointers and arrays makes a lot of C beginners have a headache, especially multidimensional arrays, that really is to kill, here I give you a good analysis of the relationship between pointers and multidimensional arrays. we all know that a one-dimensional array name is a pointer constant, which represents the address of the first element of the array, and we know the length of a one-dimensional array, you can output all elements of a one-dimensional array by the array name:
#include <stdio.h> int main (void) { int i; int a[5] = {1, 2, 3, 4, 5}; int *p = A; for (i = 0; i < 5; i++) printf ("%d\n", * (P + i)); return 0; }
However, in a multidimensional array, the array name is the address of the first array, how do you understand it? For example, define a two-dimensional array:
int a[2][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
then the array name A is the address of the first row and one-dimensional array of the two-dimensional array, instead of mistakenly thinking that it represents the address of the first element of the first line oh, how do we correctly declare a pointer to an array of shapes? Int (*p) [5] = A;it causes P to point to a one-dimensional array of the first row of the two-dimensional array (note that the first row of one-dimensional arrays is pointed to).
#include <stdio.h> int main (void) { int i; int a[2][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, ten}; int (*p) [5] = A; for (i = 0; i < 5; i++) printf ("%d\n", * (*p + i)); return 0; }
The above program also output the *p, here p is a pointer to the first line of the two-dimensional array, then the first row of the array, then the first row of the array, then we can go through * (*p + i) output of the first row array of elements. A little messy? Oh, think again, or directly say A is the first line, then *a represents the first row of the one-dimensional array, but *a is still a pointer constant, and **a is the first element of this one-dimensional array value.
What if we are going to define a pointer that can be accessed individually? The following two methods are correct declarations and assignments:int *p = &a[0][0];int *p = a[0];The first method is to simply take the first element of the first line of the array to the pointer variable p, which is probably the most intuitive method. The second method, in fact, the same principle, just said a is the first line, the first line is a one-dimensional array, then a[0] is the first element of this one-dimensional array address. Pay special attention to this a[0] is not a value, but an address.
#include <stdio.h> int main (void) { int i; int a[2][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, ten}; int *p = a[0]; for (i = 0; i < i++) printf ("%d\n", * (P + i)); return 0; }
The above code outputs all the elements in a two-dimensional array.
Attached: a[0 here] but output Hello int main (void) { char a[2][10] = {"Hello", "Hi"}; printf ("%s\n", A[0]); printf ("%s\n", *a); printf ("%s\n", *a + 1); Output "Ello" //printf ("%s\n", * (A + 1)), output "HI" return 0;
and why is that? Here the a[0] is still an address, it points to a string constant,%s can be printed address, here and%d output value is not the same, a[0] point to the first letter of the address of the string, until the output to NULL.
A summary of the contents of the C language about arrays and pointers