Elementary --4 C language extensions: Array
1. Return Value Meaning
The return value Sometimes there are two cases: the legal value and an illegal value.
If there are illegal values, the value of certain commonly used to refer to special circumstances. For example: array subscript 0 and can only be a positive number. We find the circumstances to achieve an array of time scale, there is an element not found in the next element, then, to use -1
as a return value in this case.
Return value and meaning of the definition is artificial.
2. The use of a two-dimensional array pointer
- A one-dimensional array pointer usage
No. | operating | Subscript | pointer |
---|---|---|---|
1 | The first i element of value |
arr[i] |
*(arr+i) |
2 | The first i element address |
&arr[i] |
arr+i |
- Two-dimensional array pointer usage
No. | operating | Subscript | pointer |
---|---|---|---|
1 | The first i row j column element values |
arr[i][j] |
*(*(arr+i)+j) |
2 | The first i row of the j column element address |
&arr[i][j] |
*(arr+i)+j |
In the two-dimensional array
a[i]
is a one-dimensional array.
- problem
-
Two-dimensional array array name is the first address of the first element of it?
-
Try the following code
#include <stdio.h>
int main () {
int days[4][3]={31,28,31,30,31,30,31,31,30,31,30,31};
printf("days[0]\t\t = %p\n&days[0][0]\t = %p\n",days[0],&days[0][0]);
printf("days[1]\t\t = %p\n&days[1][0]\t = %p\n",days[1],&days[1][0]);
printf("days[2]\t\t = %p\n&days[2][0]\t = %p\n",days[2],&days[2][0]);
printf("days[3]\t\t = %p\n&days[3][0]\t = %p\n",days[3],&days[3][0]);
return 0;
}
One-dimensional elements in a two-dimensional array is an array of pointers.
int days[4][3]={31,28,31,30,31,30,31,31,30,31,30,31};
int* p = days[0];
for(int i=0;i<3;++i){
printf("%d ",p[i]);
}
3. The one-dimensional array using multi-dimensional way
3.1 one-dimensional array to a two-dimensional array
int n = 24;
int arr[n];
for(int i=0;i<n;++i){
arr[i] = i;
}
for(int i=0;i<n;++i){
printf("%d ",arr[i]);
}
printf("\n");
3.2 one-dimensional array to a two-dimensional array
One-dimensional array using a two-dimensional array access method (double loop), can be seen as a box placed in accordance with the ranks of the way.
元素下标 = 当前行序号*列元素个数 + 当前列序号
int n = 24;
int arr[n];
for(int i=0;i<n;++i){
arr[i] = i;
}
int rows = 4;
int cols = 6;
for(int i=0;i<rows;++i){
for(int j=0;j<cols;++j){
printf("%d ",arr[i*cols+j]);
}
printf("\n");
}
A three-dimensional array to a 3.3-dimensional array
One-dimensional array using a three-dimensional array of access methods (triple loop), can be seen as a box placed in accordance with the ranks of high fashion.
元素下标 = 当前面序号 * 面元素个数 + 当前行序号 * 列元素个数 + 当前列序号
int n = 24;
int arr[n];
for(int i=0;i<n;++i){
arr[i] = i;
}
int pages = 2;
int rows = 4;
int cols = 3;
for(i=0;i<pages;++i){
for(j=0;j<rows;++j){
for(k=0;k<cols;++k){
printf("%d ",arr[i*rows*cols+j*cols+k]);
}
printf("\n");
}
printf("\n");
}
4. few weeks calculation
Kim Larson formula
w=(d + 2*m + 3*(m+1)/5 + y + y/4 - y/100 + y/400) % 7
- Explanation
- Formula should January and February 13, respectively, as the previous year and month 14 month treatment.
For example: January 4, 2008 to be replaced by 2007 to March 4, into the formula. - Corresponding to the formula a little different from Zeller's formula:
0
as week 1, ......,6
Sunday