Do not tangle character arrays and character pointers
Some Characteristics of character arrays and character pointers in C language are a bit fuzzy. Sometimes I only know why I want to do this, but I forget it after a while, next time, it will take time and effort to find the answer. OK. Let's take a look at it today ~
Here are two basic concepts:
1. Declare a character array
int a[10];
Defines an array a with a length of 10. In other words, it defines a set of 10 objects, which are stored in adjacent memory areas, the names are a [0], a [1],... if the pa statement is
int *pa;
It indicates that it is a pointer to an integer object, then the value assignment statement
pa = &a[0];
You can point the pointer pa to The 0th elements of array a, that is, the pa value is the address of array element a [0. In this way, the value assignment statement
x = *pa;
Copy the content in array element a [0] to variable x.
According to the pointer operation definition, pa + 1 points to the next element, and pa + I points to the I element after the pa points to the array element. The above conclusion is true regardless of the element type or array length in array. Adding 1 to the pointer means pointing to the next object.
Therefore, pa = & a [0] can also be written as follows:
pa = a;
To sum up, & a [I] And a + I have the same meaning. Correspondingly, if pa is a pointer, you can add subscript (
Yes, you are not mistaken)
Char a [100]; char * B = NULL; memset (a, 0, sizeof (a); a [0] = '0 '; a [1] = '1'; a [2] = '2'; B = a; char test = B [1]; // The value of test is 1
However, it is important to remember that the array name and pointer are different: the pointer is a variable. Therefore, in C language, the statements pa = a and pa ++ are legal. However, the array name is not a variable. Therefore, statements similar to a = pa and a ++ are invalid.
2. Character pointers
char *a = test;
Here, pointer a is a variable that points to the first address of test in the static constant area, and "test" cannot be changed in the normal state (of course, it can be changed through some means ).
With the above two points, continue now...
1. character array to character pointer
Sometimes it is necessary to convert character arrays and character pointers to each other. The strcpy () function is inevitably used. First, let's look at its source code:
void strcpy(char *s, char *t) { while ((*s++ = *t++) != '') ; }
Let's take a look at a simple example:
Char * a = NULL; char B [5] = {'1', '2'}; a = B; // OK, so that strcpy (a, B) is correct ); // memory error will be reportedAt first, the pointer variable points to NULL, And a does not allocate space in the memory. When a = B, the pointer variable a points to the first element address of the array, that is, '1 '. However, in strcpy, * s cannot be assigned because it does not open up memory space. Someone will ask, is it possible to open up the memory space? that line, I am like char * a = abc; now * a has the memory space! I tried to find that I still couldn't do it, because * a cannot be changed now. Of course, the forcible assignment is wrong. If you have to use strcpy, you have to re-open the memory to.
Char * a = (char *) malloc (100); // The size depends on your situation. char B [5] = {'1', '2 '}; strcpy (a, B); // No problem
The difference between the two methods is:
When values are directly assigned, the pointer is changed, that is, a points to array B.
When the strcpy function is used, one more space is opened in the memory. That is to say, there are two identical spaces in the memory, all of which are {'1', '2'}, one pointing by, A block is pointed by B.
2. Character pointer to character array
Char * a = 123; char B [5]; memset (B, 0, sizeof (B); B = a; // strcpy (B, a) is reported ); // OK
As mentioned above, the array name is not a variable and cannot be left value. Obviously, B = a will report an error.
The two parameters in strcpy are passed to char *. There is no problem at all. s points to the first address of B, and t points to '1' of the constant area '.
The value of a single element in the character array can be changed. Here we will talk about the problem of character array initialization. There are three situations:
1. assign values directly using strings during definition
Char a [10] = hello;
Note: you cannot assign a value to it first.
2. assign values to characters in the array one by one
Char a [10] = {'h', 'E', 'l', 'l', 'O '};
3. Use strcpy