C++ Characters, Strings, Character Arrays, String Pointers, Pointer Arrays
Category Programming Techniques
I. Character Pointers, Character Arrays
Character Pointers
A character string pointer variable is itself a variable used to store the starting address of a string. The string itself is stored in a continuous block of memory starting from this address and ends with a \0
to mark the end of the string.
char *ps="C Language";
The sequence is: 1. Allocate memory for the character pointer; 2. Allocate memory for the string; 3. Assign the starting address of the string to the character pointer;
char *ps; // ps is a character string pointer, a pointer, and a variable
ps="C Language"; // ps is the starting address of the string, using ps++ can iterate through the string, the string is stored in a continuous block of memory starting from ps, and ends with \0
.
There are two points to consider clearly here:
1. *a only points to a single character.
For example:
Example
#include <stdio.h>
#include <stdlib.h>
int main(void){
char *a= "bcd" ;
printf("Output character: %c \n", *a); /*Output character, using "%c"*/
printf("Output character: %c \n", *(a+1) ); /*Output character, using "%c"*/
printf("Output string: %s \n", a); /*Output string, using "%s"; and there should be no asterisk "*" before a */
system("pause"); /*To see the output result*/
}
The running result is as follows:
Output character: b
Output character: c
Output string: bcd
2. If a string constant appears in an expression, it represents the address of the first character of that string constant. So hello only represents its address. The original declaration is equivalent to the following declaration:
char *a;
a="hello"; /*Here the string "hello" only represents the address of its first character*/
Character Arrays
A character array is composed of several array elements and can be used to store the entire string. (i.e., using a character array to store a string).
In C language, strings are treated as character arrays. (Not in C++)
Methods for initializing character arrays:
1). A character array can be initialized using a string constant:
char str[]={"Iamhappy"};
Braces can be omitted
char str[]="Iamhappy"; # The system automatically adds \0
Note: The above method of assigning the entire string to a character array can only be used during initialization and cannot be used for assignment to a character array. Assignment to a character array can only be done element by element.
The following assignment method is incorrect:
char str[20];
str="Iamhappy";
Assign values to each element of the character array.
char str[10]={'I','','a','m','','h','a','p','p','y'};
In C language, strings can be represented and stored in two ways:
(1) Using a character array to store a string
char str[]="IloveChina";
(2) Using a character pointer to point to a string
char *str="IloveChina";
The string output for both representations uses: printf("%s\n", str);
%s
indicates outputting a string, giving the name of the character pointer variable str (for the first representation method, the character array name is the first address of the character array, which is consistent with the pointer meaning in the second representation). The system outputs the character data it points to first, then automatically increments str by 1, making it point to the next character... and so on, until the string termination identifier \0
is encountered.
II. String Pointers
string* str
can be assigned:
string* str = {"hello", "world"};
// Compared to char *name = "wang" = {'w','a','n','g'}
// *(str) = "hello", *(str+1) = "world"
// *(*(str)+1) = 'e'
This means that each element is of string type, different from char. However, string can be replaced with char**:
string = char*, string* = char**
III. (String) Pointer Array
Example
#include <stdio.h>
void main()
{
char *str[] = {"Hello", "C++", "World"}; //char (*str)[] = ...
int i;
for(i=0; i<3; i++)
printf("%s\n", str[i]);
}
// str[0] is the starting address of the string "hello", str[0]+1 is the address of the second character 'e' of the string "hello", str[2]=str+2 is the starting address of the third string "world"
// str[1] is the starting address of the string "C++"
// str[2] is the starting address of the string "world"
Or
Example
#include <stdio.h>
#include <string.h>
int main()
{
char *str[] = {"Hello", "C++", "World"};
char **p;
for(p=str; p<str+3; p++)
puts(*p); #*p is the starting address of the string, *p[0] is the address of the first character of the string
}
Example
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *str[3]={"Hello","C++","World"};
printf("%s,%s,%c",str[0],str[0]+1,*(*(str+2)+1));
system("pause");
}
The result is:
Hello,ello,o
Format: char* na[N] = {"li", "zh", "li", "zh", "li"};
char *a[]
: Indicates that a is an array, and the elements of the array are pointers to char type, (all elements of the array are stored in continuous memory). The array name is the address of the first byte of the array, and the array name a also represents the pointer. So a does not represent the content stored at the a address, but the a address 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, the required space is 8 bytes (64-bit memory address).)
*(a+1)
: Represents the content of the second element of the a array (a char pointer, represented here as the address of the world string).
*(*(a+1))
: Represents the content pointed to by the second element of the a array (a char pointer) (the w character).
char * a[10]
: Indicates that this array can store up to 10 elements (char pointers), which means this array occupies 10*8 = 80 bytes.
#w: a+1 => *(a+1) => *(a+1)[0]
Pointer (address) Pointer content (string) Character
IV
char *argv: Understand as a string
char **argv: Understand as a string pointer
char *argv[]: String pointer array
int main(int argc, char*argv[])
This is a typical example of an array name (or pointer array) being used as a function parameter, and it is also a form parameter array without specifying a size.
Sometimes, in order to process array elements in the called function, another parameter can be set to pass the number of array elements that need to be processed. And when an array name is used as a function argument, it is not the value of the array elements that is passed to the parameter, but the address of the first element of the actual array parameter, so that the two arrays share the same memory unit. This is different from the function of variables as function parameters.
You can take a look at examples of arrays as function parameters and pointer arrays as main function parameters. The book by Tan Haoqiang explains this in detail and gives a detailed explanation of this.
- When char [] is used as a parameter for a function, 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 the parameter a.
- char * a and char a[]
Similarities: a is a pointer, pointing to char type.
Differences: char a[] stores content in the stack. char *a stores the pointer in the stack and the content in constants.
- char * a[10] and char a[10][20]
Similarities: a is a second-level pointer, a represents a first-level pointer, *a represents the content stored in memory.
Differences: char * a[10], the array consists of char * type pointers; char a [10][20] indicates that one dimension holds 10 elements, the second dimension holds 20 elements, the value storage is a continuous memory area, and there is no pointer.
- Tip: The number of [] and * corresponds, such as char a[][] where the pointer level is 2, equivalent to char *a; char *a[] is also the same, two layers of pointers. When confused, count how many * and [], and you will know whether the stored content is an address or not? For example, in the case of char a[][], &a, a, *a are all addresses, *a is the content.
>
Original Source: https://www.cnblogs.com/cj2014/p/3726380.html
** Click me to share the note
-
-
-