1. Character Pointers and Character Arrays
Character Pointer
The string pointer variable itself is a variable used to store the starting address of a string. The string itself is stored in a contiguous memory space starting at that starting address and ends with\0as the end of the string.
char *ps="C Language";
means 10 elements in the first dimension and 20 elements in the second dimension, with values stored in a contiguous memory area, without pointers.
char *ps; // ps 字符串指针,是指针,是一个变量
ps="C Language"; // ps is the starting address of the string. Use ps++ to traverse the string. The string is stored in a contiguous memory space starting at ps, and ends with\0as the end of the string.
There are two points to consider clearly here:
1. *a simply points to a character.
An example is as follows:
Example
The running result is as follows:
输出字符:b 输出字符:c 输出字符串:bcd
2. If a string constant appears in an expression, the value it represents is the address of the first character of that string constant. So "hello" merely represents its address. The original declaration is equivalent to the following declaration:
char *a; a="hello"; /*这里字符串"hello"仅仅代表其第一个字符的地址*/
Character Array
A character array is composed of several array elements and can be used to store an entire string. (That is, use a character array to store a string.)
In C, strings are treated as character arrays. (Not in C++.)
Methods for initializing a character array:
1). A character array can be initialized with a string constant:
char str[]={"Iamhappy"};
The curly braces can be omitted.
char str[]="Iamhappy"; # 系统自动加入 \0
Note: The above whole-array assignment can only be used during initialization of a character array, not for assignment to a character array. Assignment to a character array can only be done element by element.
The following assignment method is wrong:
char str[20]; str="Iamhappy";
Assign values to each element of a character array one by one.
char str[10]={'I','','a','m','','h','a','p','p','y'};
In C, there are two ways to represent and store strings:
(1) Use a character array to store a string
char str[]="IloveChina";
(2) Use a character pointer to point to a string
char *str="IloveChina";
Both representation methods use the following to output strings:printf("%s\n", str);
%sTo output a string, give the character pointer variable name str (for the first representation, the character array name is the starting address of the character array, which has the same meaning as the pointer in the second). The system first outputs the character data it points to, then automatically increments str by 1 to point to the next character... and so on, until the string terminator is encountered.\0 。
2. String Pointers
string* strCan be assigned:
string* str = {"hello", "world"};
// 对比与 char *name = "wang" = {'w','a','n','g'}
// *(str) = "hello", *(str+1) = "world"
// *(*(str)+1) = 'e'
That is, each element is of type string, which is different from char*. However, string* can be replaced by char**:
string = char*, string* = char**
3. (String) Pointer Arrays
Example
or
Example
The result is:
Hello,ello,o
Format:char* na[N] = {"li", "zh", "li", "zh", "li"};
char *a[]: indicates that a is an array whose elements are pointers to char type (all elements in the array are stored in contiguous memory). The array name is the memory address of the first byte of the array, and the array name a also represents a pointer. So a does not represent the content stored at address a, but the address a itself.
a+1: represents the memory address of the second element of a, so it adds 8 bytes. (Because the elements of a are char pointers, requiring 8 bytes (64-bit memory address).)
*(a+1): indicates the content of the second element of array a (which is a char pointer, represented as the address of the "world" string in this example).
*(*(a+1)): indicates the content (the character w) pointed to by the content (char pointer) of the second element of array a.
char * a[10]: indicates that this array can hold at most 10 elements (char pointers), meaning the array occupies 10*8 = 80 bytes.
#w: a+1 => *(a+1) => *(a+1)[0]
指针(地址) 指针内容(字符串) 字符
four
char *argv:理解为字符串 char **argv:理解为字符串指针 char *argv[]:字符串指针数组
int main(int argc, char*argv[])This is a typical example of an array name (or pointer array) used as a function parameter, and it is also a formal parameter array without a specified size.
Sometimes, in order to handle array elements in the called function, an additional formal parameter can be set to pass the number of array elements to be processed. Moreover, when using an array name as an actual parameter, it is not the values of the array elements that are passed to the formal parameter, but the address of the first element of the actual parameter array is passed to the formal parameter array, so that the two arrays share the same memory location. This is different from using variables as function parameters.
You can look at examples of arrays as function parameters and pointer arrays as main function formal parameters. Tan Haoqiang's book explains this in great detail.
1. When char [] is used as a function parameter, it means char *. When passed as a function parameter, it actually copies the address of the first element of the array.
So void test(char a[]) is equivalent to void test(char *a)
char x[10] ;
Then calling test(x) is equivalent to assigning the address of the first element of x to parameter a.
2. char *a and char a[]
- Similarity: a is a pointer pointing to char type.
- Difference: char a[] stores the content on the stack. char *a stores the pointer on the stack and the content in constants.
3. char *a
- Similarity: a is a two-level pointer in both cases; *a represents a one-level pointer; **a represents the content stored in memory.
- Difference: char *a
4. Tip: The number of [] and * correspond. For example, char a[][] has 2 pointer levels, equivalent to char **a; char *a[] is also the same, two levels of pointers. When confused, count how many * and [] there are, and you will know whether content or address is stored. For example, in the case of char a[][]: &a, a, *a are all addresses, while **a is content.
Original URL: https://www.cnblogs.com/cj2014/p/3726380.html