[求助]陣列的原理
Arrays
----------
*An array is a group of memory locations related by the fact that they all have the same name and the same type.
*To refer to a particular location or element in the array, we specify:
1. the name of the array
2. the position number of the particular element
*E.g.
+----+----+----+----+----+----+----+----+----+----+----+----+
| -45| 6 | 0 | 72 |1543|-89| 0 | 62 | -3 | 1 |6453| 78 |
+----+----+----+----+----+----+----+----+----+----+----+----+
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11]
*The first element in every array is the zeroth element; i.e., "c[0]".
* In general, the ith element of array "c" is referred to as "c[i-1]".
* The position number (must be an integer or an integer expression) contained within square brackets is more formally called a subscript, or index.
*E.g. a=5, b=6
c[a+b] += 2;
adds 2 to array element c[11].
Declaring Arrays
-------------------------
*The programmer specifies the types of each element and the number of elements required by each array.
*The computer reserve the appropriate amount of memory.
*E.g.
int c[12];
is used to tell the computer to reserve 12 elements for integer array "c".
*E.g.
int b[100], x[27];
char str[50];
float reading[200];
*E.g. array1.c
#include
int main()
{
int n[10], i;
for (i=0; i<=9; i++)
n = 0;
printf("%s%13s\n", "Element", "Value");
for (i=0; i<10; i++)
printf("%7d%13d\n", i, n);
return 0;
}
*The elements of an array can also be initialized in the array declaration by following the declaration with an equals sign and a comma-separated list (enclosed in braces) of initializers.
*E.g. array2.c
#include
int main()
{
int i, n[10] = {32, 27, 64, 18, 95, 14, 90, 70, 60, 37};
printf("%s%13s\n", "Element", "Value");
for (i=0; i<10; i++)
printf("%7d%13d\n", i, n);
return 0;
}
*If there are fewer initializers than elements in the array, the remaining elements are automatically initialized to zero.
*It is important to remember that arrays are not automatically initialized to zero, just like other variables.
* If the array size is omitted from a declaration with an initializer list, the number of elements in the array will be the number of elements in the initializer list.
*E.g. five-element array
int n[] = {1, 2, 3, 4, 5};
*E.g. array3.c
#include
#define SIZE 10
int main()
{
int s[SIZE], j;
for (j=0; j <= SIZE - 1; j++)
s[j] = 2 + 2 * j;
printf("%s%13s\n", "Element", "Value");
for (j=0; j
#define SIZE 12
int main()
{
int a[SIZE] = {1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45},
i, total = 0;
for (i=0; i;
printf("Total of array element values is %d\n", total);
return 0;
}
*A string such as "hello" is really an array of individual characters in C.
*E.g.
char string1[] = "first";
*It is important to note that the string "first" contains five characters plus a special string termination character called the null character (';\0';).
*Thus, array "string1" actually contains six elements.
*All strings in C end with the null character.
*A character array representing a string should always be declared large enough to hold the number of characters in the string and the terminating null character.
*E.g. string1[0] is the character ';f'; and string1[3] is the character ';s';.
*We also can input a string directly into a character array from the keyboard using "scanf" and the conversion specification "%s".
*E.g.
char string2[20];
scanf("%s", string2);
*The name of the array is passed to "scanf" without the preceding "&" used with other variables.
*"scanf" can write beyond the end of the array.
*A character array representing a string can be output with "printf" and the "%s" conversion specifier.
*E.g.
printf("%s\n",string2);
*The characters of the string are printed until a terminating null character is encountered.
*E.g. string.c
#include
int main()
{
char string1[20], string2[] = "string literal";
int i;
printf("Enter a string: ");
scanf("%s", string1);
printf("string1 is: %s\nstring2 is: %s\n"
"string1 with spaces between characters is:\n",
string1, string2);
for (i=0; string1 != ';\0';; i++)
printf("%c ", string1);
printf("\n");
return 0;
}
*In functions that contain automatic arrays where the function is in and out of scope frequently, make the array "static" so it is not created each time the function is called.
Passing Arrays to Functions
---------------------------------------
*To pass an array argument to a function, specify the name of the array without any brackets.
*E.g.
int hourlyTemp[24];
....
modifyArray(hourlyTemp,24);
....
*When passing an array to function, the array size is often passed so the function can process the specific number of elements in the array.
*The name of the array is actually the address of the first element of the array.
* Because the starting address of the array is passed, the called function knows precisely where the array is stored.
*Therefore, when the called function modifies array elements in its function body, it is modifying the actual elements of the array in their original memory locations.
*E.g.
#include
int main()
{
char array[5];
printf("array = %p\n&array[0] = %p\n", array, &array[0]);
return 0;
}
*Although entire arrays are passed simulated call by address, individual array elements can be passed call by values exactly as simple variables are.
*E.g.
#include
int main()
{
char str[]="hello";
foobar(str[3]);
....
}
*For a function to receive an array through a function call, the function';s parameter list must specify that an array will be received.
*E.g.
void modifyArray(int b[], int size)
{
int j;
for (j=0; j
void tryToModifyArray(const int []);
int main()
{
int a[] = {10, 20, 30};
tryToModifyArray(a);
printf("%d %d %d\n", a[0], a[1], a[2]);
return 0;
}
void tryToModifiyArray(const int b[])
{
b[0] /= 2; /* error */
b[1] /= 2; /* error */
b[2] /= 2; /* error */
}
Multiple-Subscripted Arrays
------------------------------------------
*A common use of multiple-subscripted arrays is to represent tables of values (in rows and columns).
*Tables or arrays that require two subscripts (indexes) to identify a particular element are called double-subscripted (two-dimensional) arrays.
*Every element in array "a" is identified by a name of form "a[j]": "a" is the name of the array, and "i" and "j" are the indexes that uniquely identify each element in "a".
*A multiple-dimensional array can be initialized:
int b[2][3] = {{1,2,3}, {4,5,6}};
*E.g. multiarray.c
#include
void printArray(int [][3]);
int main()
{
int array1[2][3] = {{1,2,3},{4,5,6}},
array2[2][3] = {1,2,3,4,5},
array3[2][3] = {{1,2},{4}};
printf("Values in array1 by row are:\n");
printArray(array1);
printf("Values in array2 by row are:\n");
printArray(array2);
printf("Values in array3 by row are:\n");
printArray(array3);
return 0;
}
void printArray(int a[][3])
{
int i,j;
for (i=0;i<2;i++)
{
for (j=0;j<3;j++)
printf("%d ", a[j]);
printf("\n");
}
}