Array
You can store a variable of one or more sets of values, which contains multiple elements, and each member of the array is an array element.
One-dimensional arrays
Definition: type array name [constant expression] = {value 1, value 2, value 3 ...};
int a[3] = {0, 1, 2};
Float F[2] = {1.2, 2.3, 3.14};
Char str[] = {' h ', ' e ', ' l ', ' l ', ' o '};
Chat str1 = "IPhone"; This is also a method of defining character arrays, which are explained in detail later in this section.
When the number of array elements is variable, only the array can be declared and cannot be directly assigned:
int length = 4;
int a[length]; This can be
int a[length] = {2, 4, 6, 8};//this will be an error.
Precautions:
1. The system will not detect whether the subscript of the array elements is out of bounds, and when programming, you must ensure that the array subscript cannot be crossed.
2, cannot define array int array[] = {}; Array number and initial value cannot be empty at the same time
3, also cannot define array int array[2] = {1, 2, 3}; The number of values in an array is greater than the number of arrays
4, the array is a whole, can not directly participate in the operation, can only handle a single element, usually used in the place of the array will use the loop
Common Array Operations
If an array is initialized, the number of elements in the array is determined, and the constant expression can be written without:
int a[] = {1, 3, 5, 7, 9};//array length is 5 equivalent to int a[5] = {1, 3, 5, 7, 9};
An int is an array type, which means that each element in the array is of type int.
Access: The element in the array is accessed through the array name + subscript, and note that the subscript of the array starts at 0, A[0] is the first element, A[1] represents the second element, and A[2] represents the third element. So the last element of the array is the length of the array –1, do not write a[3], so that is the array out of bounds.
In the array access we usually operate through a for loop, which is generally called traversal
int ages[28];
Circular assignment
for (int i = 0; i < i++)
{
int age = arc4random ()% 13 + 18;//The range of everyone's ages is: 18 ~ 30 of the random value
ages[ I] = age;//assign value to each element
}
//Access, also called Traversal for
(int i = 0; i < i++)
{
printf ("a[%d] =%d", I, ages[i]); c13/>}
Each element of the two array is added, added, and stored in the third array:
int a1[] = {2, 4, 5, 6,};
int a2[] = {1, 3, 4, 5, 6};
int a3[5];
for (int i = 0; i < 5; i++)
{
A3[i] = A1[i] + a2[i];
printf ("a3[%d] =%d", I, A3[i]);
The output results are:
A3[0] = 3 A3[1] = 7 A3[2] = 9 a3[3] = one a3[4] = 16
How the length of the array is calculated:
sizeof (array name)/sizeof (array first element)
int arr[] = {2, 3, 4, 6, 7};
int length = sizeof (arr)/sizeof (arr[0));
printf ("Length =%d\n", length);
The output results are:
Character arrays: Arrays used to hold character variables, called character arrays, have two defining formats:
Char str[4] = {' A ', ' B ', ' C ', ' d '};
Char str2[] = "iPhone";//Another way
Output of string:
printf ("str2 =%s\n", str2);//%s output string, encountered ' the ' end of '
An array of characters with a ' I ' end identity is also known as a string, and the system provides a number of string functions
size_t strlen (const char *): Computes the string length, is the valid length of the string, not counting ' \0′
Char str[] = "IPhone";
unsigned long length = strlen (str);//strlen indicates effective length
printf ("Length =%lu\n", length);
The output results are:
The principle of strlen () is to traverse the string and end when it encounters ' \0′.
Char *strcpy (char *, const char *): string copy
Char str1[] = "Unity";
Char str2[] = "Android ...";
printf ("%s\n", strcpy (STR1, str2));
Output results:
Char *strcat (char *, const char *): string concatenation
Char str1[] = "Unity";
Char str2[] = "IPhone";
printf ("%s\n", strcat (STR1, str2));
Output results:
int strcmp (const char str1*, const char str2*): string comparison, and returns a negative integer, 0, or positive integer, respectively, according to the str1 result of a dictionary order less than, equal to, or greater than str2. The return value is the difference between the characters at the first unequal character encountered by the STR1 and str2 when compared to the front and back characters
Char str1[] = "Unity";
Char str2[] = "IPhone";
printf ("%d\n", strcmp (STR1, str2));
Output results:
Copy Code code as follows:
Pointer Access array
if the PA points to an element in the array, then pa+1 points to the next element, Pa+i to the down I element. So, if the PA points to a[0], then *PA is actually a[0],* (pa+1) is a[1],* (Pa+i) is a[i]. Based on this idea, we can use pointers to access arrays.
#include <stdio.h>
int main (int argc, char *argv[])
{
int score[10]={76,85,90,67,59,79,82,95,91,65 };
int wait, length, I;
length = sizeof (score)/sizeof (score[0));
for (i = 0; i < length; i++)
{
printf ("%d \ n", * (score + i));
}
scanf ("%d", &wait);
}
Program output:
The array subscript is closely related to the pointer. After executing the statement PA = &a[0], the PA and a have the same value, and the *pa=a[0]. In fact, the C language also uses pointers to compute the array subscript. The subscript and the pointer are actually equivalent.
When an array name is passed to a function, the address of the first element of the array is actually passed. Therefore, the array name parameter must be a pointer.
A program that calculates the length of a string can be written as follows:
int strlen (char *s)
{
int n;
for (n = 0; *s!= '; s++ ')
n++;
return n;
}
S is the pointer, which is a variable, it is legal to increase it.