Overview
The array has a special place in the C language, it has many features, such as its storage is continuous, the name of the array is the address of the array. In the C language there is no string type, so if you want to represent a string, you must use an array of strings. Today, mainly on the following three aspects:
One-dimensional array Multidimensional array string
One-dimensional arrays
One-dimensional array operation is simpler, but it needs to be noted that the length of the array must be fixed, the length cannot be initialized with a variable, and the length of the array can be omitted if the declaration is assigned at the same time, and the compiler automatically evaluates the array length, and the array cannot declare a one-time assignment (which of course can be assigned to one by one for each element).
#include <stdio.h>
int main () {
int len = 2;
int A[len] = {1, 2};//error, cannot make variable
int a[2];//correct
a[0] = 1;
A[1] = 2;
A[2] = 3;//exceeds array length, but the compiler does not check, run error
int b[' a ' = {1,2,3};//' a ' = 97, so it can be an array length, but the following element is not initialized and its value defaults to 0 for
(int i = 0; I < 97; ++i) {
printf ("b[%d]=%d\n", I,b[i]);
int c[2 * 3];//2*3 is fixed value can be used as array length
int d[] = {1, 2, 3};//If the initialization of the simultaneous assignment then the array length can be omitted, the current number is 3
}
Extensions--Storage of arrays
An array is stored in memory in a contiguous space, if you know the array type (int, float, and so on) and the initial address, you can know the address of the other element, and because the array name equals the address of the first element of the array, it is actually a reference pass when the array as a parameter (as the parameter can be omitted).
#include <stdio.h>
int main () {
int const L = 3;
int A[l] = {1, 2,3};
for (int i = 0; i < L; ++i) {
//due to the current 32-bit compiler, the int-type length is 4 bytes, you can tell that three address 22 is 4
printf ("a[%d]=%d,address=%x\n", I, a [i], &a[i]);
}
/* Current output:
a[0] = 1, address = c9f95c
a[1] = 2, address = c9f960
a[2] = 3, address = c9f964*/
}
Let's take a look at the array defined above to store the structure in memory
Let's take a look at the case where the array is passed as a parameter (note that the elements of the array are not arrays)
#include <stdio.h>
void changevalue (int a[]) {
a[0] = ten;
}
int main () {
int a[2] = {1,2};
ChangeValue (a);
for (int i = 0; i < 2; ++i) {
printf ("a[%d]=%d\n", I,a[i]);
/* Print results
a[0]=10
a[1]=2/
}
Multidimensional arrays
Multidimensional arrays can actually be considered as a special one-dimensional array, but each element is a one-dimensional array, and the following is a simple look at the initialization and assignment of multidimensional arrays
#include <stdio.h> int main () {int A[2][3];//2 row 3 columns, a two-dimensional array can be viewed as a special one-dimensional array, except that each element of it is a one-dimensional array a[0][0] = 1;
A[0][1] = 2;
A[0][2] = 3;
A[1][0] = 4;
A[1][1] = 5;
A[1][2] = 6; for (int i = 0; i < 2; ++i) {for (int j = 0; j < 3; ++j) {printf ("a[%d][%d]=%d,address=%x\n", I, J, A[i][j
], &a[i][j]);
}/* Print results a[0][0]=1,address=f8fb24 a[0][1]=2,address=f8fb28 A[0][2]=3,ADDRESS=F8FB2C A[1][0]=4,address=f8fb30
A[1][1]=5,ADDRESS=F8FB34 a[1][2]=6,address=f8fb38//initialization and direct assignment int b[2][3] = {1, 2, 3}, {4, 5, 6}}; Because the order of the arrays is assigned first from the first column, then the second column in the first row ...
Then the second row of the first column ..., so we can also write the following form int c[2][3] = {1, 2, 3, 4, 5, 6};
You can also initialize only some of the data, and the remaining elements default to 0 int d[2][3] = {1, 2, 3, 4};
for (int i = 0; i < 2; ++i) {for (int j = 0; j < 3; ++j) {printf ("d[%d][%d]=%d\n", I, J, D[i][j]); }/* Print results d[0][0]=1 d[0][1]=2 d[0][2]=3 d[1][0]=4 d[1][1]=0 d[1][2]=0///The following assignment can also be int e[2][3] =
{{}, {4, 5, 6}}; Can omitLine number, but it is absolutely not possible to omit the column number, because it cannot determine how many rows int F[][3] = {{1,2,3},{4,5,6}}, according to the order of assignment mentioned above; }
Extensions--Storage of multidimensional arrays
Take the above a array as an example, its structure in memory is shown in the following diagram
Based on the storage of the above and one-dimensional arrays, we can conclude that the array name is the address of the entire two-dimensional array, equal to the address of the first row array name, and equal to the address of the first element, and the second row array name equals the address of the first element in the second line. Represented by an expression:
A=a[0]=&a[0][0] a[1]=&a[1][0]
About multidimensional arrays, in fact, you can, and so on, here no longer repeat.
String
There is no string type in C, and if you want to represent a string you need to use an array of type char, because the string itself is a combination of multiple characters. But note that the string is a special array, the end of which must be added a "" "(ASCII 0 is an empty operator, means nothing) to represent the end of the string, or the compiler is not know when the string has ended. When a string assignment is used directly, the program automatically adds "Yes" as the Terminator.
//MAIN.C
//arrayandstring////
Created by
kenshincui on 14-7-06.
Copyright (c) 2014 Kenshin Cui. All rights reserved.
#include <stdio.h>
int main (int argc, const char * argv[])
{
char a[] = {' K ', ' e ', ' n ', ' s ', ' h ', ', ' I ', ' n ', ' I '};
printf ("%s", a); Results: Kenshin, note that using%s output string content, if you replace the shape output format in fact, the address of A is
printf ("\ n");
printf ("address=%x", a); Result: address=5fbff890
printf ("\ n");
The following can never be omitted, if there is no b[will appear as follows
char = = {' I ', ' a ', ' m '};
printf ("%s", b); Not in accordance with the expected output, more garbage data, in the current environment printing results: Iamkenshin
printf ("\ n");
printf ("address=%x", b); Result: address=5fbff88d
printf ("\ n");
The direct assignment is a string, this time no manual addition is required, and the compiler automatically adds
char c[] = "Kenshin";
printf ("c=%s", c); Result: C=kenshin
printf ("\ n");
Two-dimensional arrays store multiple strings
char d[2][3]={"Kenshin", "Kaoru", "Rose", "Jack", "Tom", "Jerry"};
return 0;
}
From the above code comments can be seen in print B is not directly printed out "Iam" but printed "Iamkenshin", because the compiler can not determine whether the end of the string, to explain why print out "Iamkenshin" we need to understand A and B in memory storage.
It is easy to see from the graph that because a occupies 8 bytes, after the definition of a directly defined B, at this time the allocated space is continuous, B occupies 3 bytes, so when the output B due to output after the "Iam" did not Encounter "" "Mark, the program continues to output until encountered in the array a" "" to end, So the output is "Iamkenshin".
Extensions--common functions for string manipulation
Here's a quick look at several commonly used functions related to character and string
MAIN.C//arrayandstring////Created by Kenshin Cui on 14-7-04. Copyright (c) 2014 Kenshin Cui.
All rights reserved. #include <stdio.h> int main (int argc, const char * argv[]) {/* character operation/Putchar (' a ');//Result: A,putchar can output only one word at a time
Character printf ("\ n");
Putchar (97);//Result: a printf ("\ n");
Char A;
A=getchar ();//getchar () can receive only one character at a time, receive space, tab, carriage return printf ("a=%c", a);
printf ("\ n");
/* String Operation * * Char b[]= "Kenshin";
printf ("b=%s", b);
printf ("\ n"); Puts (b);
Puts is used to output a single string, not to format the output as printf, and to automatically add a newline printf ("\ n");
Char c[10]; scanf ("%s", c);/note C is not necessary to write &c, because C itself represents the address of the array printf ("c=%s\n", c);/Note that even if you input more than 10, you can output the correct, but the following gets () function is not printf
("\ n");
Gets () function, note that it is unsafe, because the receiver does not know its size is prone to overflow, it is recommended not to use Char d[10]; Gets (d);
Gets can receive only one string at a time, but scanf can receive multiple, scanf cannot receive spaces, Tab,gets may printf ("d=%s", D);
printf ("\ n");
Char e[]={' K ', ' s ', ' n '}; printf ("%lu", strlen (e));
The result is: 2, not 3, because the is not counted into the length printf ("\ n");
Char f[]={"Kenshin"}; printf ("%lu", strlen (f)); //The result is: 7 printf ("\ n");
Char g[5];
strcpy (g, "hello,world!"); printf ("%s", g);
The result: Hello, even if the defined G length is 5, it can be fully copied into printf ("\ n");
Char h[5];
Char i[]={' A ', ' B ', ' C ', ' yes ', ' d ', ' e ', ' f ', ' all '};
strcpy (H,i); printf ("%s", h);
As a result: ABC, when encountering the first one, ends printf ("\ n");
strcat (i, "ghi"); printf ("%s", I); The result is: Abcghi, note is not abcdefghi,strcat, start with "Ghi" cover from I first, after the overlay is added a, in memory should now be: {' A ', ' B ', ' C ', ' g ', ' h ', ' I ', ' ' ', ' f ', '
0 '} printf ("\ n");
Char j[]= "ABC";
Char k[]= "ABc";
Char l[]= "ACB";
Char m[]={' A ', ' I '};
printf ("%d,%d,%d", strcmp (J,k), strcmp (k,l), strcmp (l,m));//encounters the first distinct character or the difference between the two, the result: 32,-33,99 printf ("\ n");
return 0; }
Note that gets is unsafe in Xcode because Xcode is using the GCC compiler and the gets () function is not compiled correctly in the GCC compiler, and fgets () is recommended.