Array of C languages
An instance of an array declaration: int num[3]; just write down this template.
It is not recommended to use a variable to define an array, if the variable is used to define an array, as the number of elements of the array, the uninitialized case is a random value, if the initialization will directly error
Note: If the definition is initialized at the same time, then the number of elements can be omitted
After omitting, the initialization of the assignment of several data, then the length of the array is a few, that is, the array will be able to store a few data.
int scores[] = {1,3};//has only two storage space
If you do not initialize, you cannot omit the number of elements:
Wrong: int scores[];
Assignment can be specified by [] Index
int scores[100] = {[87] = 1,[34] = 29};
The length of the array can be obtained with sizeof
1#include <stdio.h>2 3 intMainintargcConst Char*argv[]) {4 5 inta[5];6printf"length of array:%i\n",(int)(sizeof(a)/sizeof(a[0])));7 return 0;8}
When the array name is used as a function parameter, the number of elements in the divisor group cannot be dynamically computed in the function because the auto-conversion is for the pointer type
1 voidPrintArray (intarray[])2 {3printf"PrintArray size =%lu\n",sizeof(array));//84 intLength =sizeof(array)/sizeof(int);//25printf"length =%d", length);6}
Xiao He said two words:
1. It seems that the method of dynamically calculating the length of the array cannot be programmed in C language.
2. Arrays in Java can be as follows:
public class main{
public static void Main (string[] args) {
int n = 10;
int a[] = new Int[n];
System.out.println (a.length);
}
However, in the C language, it is not possible to use variables such as the above Java code to dynamically specify the length of the array declaration.
int a = 10;
int B[a];
In the Java language, however, the declaration of an array cannot specify a length, that is, int[3] A or int a[3],java can only specify the length of the array at initialization time.
Declaring an array in C + + is the same as the C language, declaring the format:
Data type array name [constant expression] For example: int array[10]; constant expressions can include integer constants and integral expressions, but not variables.
The C + + and C languages do not allow dynamic definition of the size of an array.
In Swift and objective-c, the length of the array is dynamically variable.
3. However, when the C language accesses an array, the specified index can be specified with a variable. such as
int a[3]={1,2,3};
int n = 2;
printf ("%d\n", A[n]);
Small what to say two exercises of a knowledge highlight:
Although this is a very simple exercise, but the small ideas can be accumulated.
# #1. Design a function int arrayMax (int a[], int count) to find the maximum value of an array element
1 intGetmax (intAges[],intlength)2 {3 //Note: Do not assume that the maximum value of a value other than an array can cause unexpected problems4 //int max = 0;5 //assume that the No. 0 element in the array is the maximum6 intmax = ages[0];7 8 for(inti =0; i < length; i++) {9 //determines whether the value taken from the array is greater than MaxTen if(Max <Ages[i]) { One //sets the element corresponding to the current index to the maximum value if it is greater than Max AMax =Ages[i]; - } - } the - returnMax; -}
1 intGetmax (intAges[],intlength)2 {3 //The No. 0 index in the array as the maximum value4 intMax =0;//is an index5 for(inti =1; i < length; i++) {6 if(Ages[max] <Ages[i]) {7Max =i;8 }9 }Ten returnAges[max]; One}
The previous algorithm was also my idea, but I took the ternary operator instead of the conditional statement, although the code seemed to be streamlined, but nothing changed in nature.
Then I see the second algorithm, compared to the first algorithm thought, is directly oriented to maximum value programming, and the second algorithm is for the maximum value of the index programming, that is, if this value is the largest, then I would like to manipulate this is worth the index, anyway, through the index will be able to get this maximum value.
However, the second idea of an index-oriented writing algorithm is generally used in situations where the set element is not variable, and if the set changes in the algorithm, it is possible to get an incorrect value by index.
Well, read here, whether the reader is to re-read this paragraph or the first time to read this paragraph is trying not to look at the source code for the index to write this small problem algorithm.
The following is the author's practice code:
1#include <stdio.h>2 3 //design a function int arrayMax (int a[], int count) to find the maximum value of an array element4 //element-oriented specific value write algorithm5 intArrayMax (intA[],intcount)6 {7 intmax = a[0];8 for(intI=0; i<count; i++) {9Max = (Max>a[i])?Max:a[i];Ten } One returnMax; A } - - //index-oriented write algorithm the intARRAYMAX2 (intA[],intcount) - { - //defines the index of a maximum value, for example, where the maximum index is the first in the array - intMax =0; + - for(intI=0; i<count; i++) { +Max = (A[max]>a[i])?max:i; A } at returnA[max]; - } - - intMainintargcConst Char*argv[]) { - inta[5] = { A,2,2,3, at}; -printf"Maximum value is:%d\n", ArrayMax2 (A,5)); in return 0; -}
The algorithm of index-oriented value writing can play a role in reducing the spatial complexity of the algorithm in the use of the collection class.
Because the space occupied by an index is often the space occupied by Int.
Learn the array of C languages