Strings are one of the most commonly used data types.
In C, there is no String to store strings; strings are treated as a sequence of char types.
So there are two representations of strings, character arrays and character Pointers, and the two representations are initialized differently.
To facilitate the comparison of results, define the global variable LENGTH as 15, and define the output function print as follows:
/** Display the outputs. * args: chars[], The char array to print. * length, The size of the char array you wanna print. * type, Tht format you wanna print those chars. * 16: hexadecimal * 0: chars */
void print(char* chars, int length, int type){
// printf("%ld:", sizeof(chars));
if (type == 16)
{
for (int i = 0; i < length; i++)
{
printf("0x%x ", *(chars + i)); }}else if (type == 0)
{
for (int i = 0; i < length; i++)
{
printf("%c", *(chars + i)); }}printf("\n");
}
Copy the code
If type is 0, the output is a character string; if type is 16, the output is in hexadecimal format.
A character array
Assign to a string
char array_1[LENGTH] = "array_1";
print(array_1, LENGTH, 16);
printf("%ld\n".sizeof(array_1));
Copy the code
Let’s look at the results
0x61 0x72 0x72 0x61 0x79 0x5f 0x31 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
15
Copy the code
In this case, the string ends at 0, and the unassigned portions are also initialized to char 0.
withstrcpy
The assignment
char array_2[LENGTH];
strcpy(array_2, "array_2");
print(array_2, LENGTH, 16);
printf("%ld\n".sizeof(array_1));
Copy the code
The results for
0x61 0x72 0x72 0x61 0x79 0x5f 0x32 0x0 0xfffffff0 0x6c 0x7f 0x0 0x0 0xffffffc0 0xfffffff3
15
Copy the code
As you can see, using the strcpy function assignment, the string ends at \0, after which the data, type, and value are random.
Character arrays are not initialized to all zeros when declared.
Assign to a character array
In C, the flag bit at the end of a string is \0. Should \0 be written at the end of the character array used for initialization?
Case without \0:
char array_3[LENGTH] = {'a'.'r'.'r'.'a'.'y'.'_'.'3'};
print(array_3, LENGTH, 16);
printf("%ld\n".sizeof(array_1));
Copy the code
The results for
0x61 0x72 0x72 0x61 0x79 0x5f 0x33 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
15
Copy the code
Case without \0
char array_4[LENGTH] = {'a'.'r'.'r'.'a'.'y'.'_'.'4'.'\ 0'};
print(array_4, LENGTH, 16);
printf("%ld\n".sizeof(array_1));
Copy the code
The results for
0x61 0x72 0x72 0x61 0x79 0x5f 0x34 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
15
Copy the code
As you can see, there is no difference between the two methods. When directly assigned, the array initializes each position to \0 for the declared length.
It is worth mentioning
- Variables declared as arrays cannot be declared and then assigned by constants. Neither of the following versions will compile.
char array_5[LENGTH];
array_5 = "test";
array_5 = {'t'.'e'.'s'.'t'};
Copy the code
- In addition, with
sizeof
It’s actually the size of the pointer variable, which is the length of the array declaration. But the use ofstrlen
Function to find the effective length of the string, namely, to\ 0
The length of theSelf-verification.
A character pointer
When declaring a variable as a pointer, there are only two methods of assignment.
Assign directly to a string
char* pointer_1 = "pointer_1";
print(pointer_1, LENGTH, 16);
printf("%ld\n".sizeof(pointer_1));
Copy the code
The results for
0x70 0x6f 0x69 0x6e 0x74 0x65 0x72 0x5f 0x31 0x0 0x70 0x6f 0x69 0x6e 0x74
8
Copy the code
Note that sizeof now determines the effective length of the string.
Assign indirectly as a string
char* pointer_5;
pointer_5 = "pointer_5";
print(pointer_5, LENGTH, 16);
printf("%ld\n".sizeof(pointer_1));
Copy the code
The results for
0x70 0x6f 0x69 0x6e 0x74 0x65 0x72 0x5f 0x35 0x0 0x0 0x1 0x1b 0x3 0x3b
8
Copy the code
Other methods
-
When the **strcpy function is used to assign **, the compiler prompts that the pointer variable is not initialized.
You can see that when you declare as an array, the address space is already there, even though the values in the array are not initialized to 0.
With pointer declaration, there is no specified length, naturally there is no way to apply for space.
-
As for the character array assignment method, since variables are now declared as character Pointers, different types, naturally not feasible.
Complete test code
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define LENGTH 15
/** Display the outputs. * args: chars[], The char array to print. * length, The size of the char array you wanna print. * type, Tht format you wanna print those chars. * 16: hexadecimal * 0: chars */
void print(char* chars, int length, int type){
// printf("%ld:", sizeof(chars));
if (type == 16)
{
for (int i = 0; i < length; i++)
{
printf("0x%x ", *(chars + i)); }}else if (type == 0)
{
for (int i = 0; i < length; i++)
{
printf("%c", *(chars + i)); }}printf("\n");
}
int main(a)
{
// Define as array;
char array_1[LENGTH] = "array_1";
print(array_1, LENGTH, 16);
printf("%ld\n".sizeof(array_1));
char array_2[LENGTH];
strcpy(array_2, "array_2");
print(array_2, LENGTH, 16);
printf("%ld\n".sizeof(array_1));
char array_3[LENGTH] = {'a'.'r'.'r'.'a'.'y'.'_'.'3'};
print(array_3, LENGTH, 16);
printf("%ld\n".sizeof(array_1));
char array_4[LENGTH] = {'a'.'r'.'r'.'a'.'y'.'_'.'4'.'\ 0'};
print(array_4, LENGTH, 16);
printf("%ld\n".sizeof(array_1));
// char array_5[LENGTH];
// array_5 = "test";
// Define as pointer.
char* pointer_1 = "pointer_1";
print(pointer_1, LENGTH, 16);
printf("%ld\n".sizeof(pointer_1));
// char* pointer_2;
// strcpy(pointer_2, "pointer_2");
// print(pointer_2, LENGTH, 16);
// char* pointer_3 = {'p', 'o', 'i', 'n', 't', 'e', 'r', '_', '3'};
// print(pointer_3, LENGTH, 16);
// char* pointer_4 = {'p', 'o', 'i', 'n', 't', 'e', 'r', '_', '3', '\0'};
// print(pointer_4, LENGTH, 16);
char* pointer_5;
pointer_5 = "pointer_5";
print(pointer_5, LENGTH, 16);
printf("%ld\n".sizeof(pointer_1));
return 0;
}
Copy the code