The difference between a string and an array of characters, and how to define an array of characters

Updated on technology 2024-02-26
10 answers
  1. Anonymous users2024-02-06

    There is no difference between a string and an array of characters.

    char *a;It is the pointer.

    There is no such form char*a("asdfadf");

    char a[8] = "asdfadf";and char *a="asdfadf";The effect is the same.

    For example: char *a="asdfadf";Declaration and initialization.

    int i;

    for (i=0;iprintf("");

    printf("%s",a);Hit asdfadfprintf("%s",a+2);Hit dfadf as well: char a[8]="asdfadf";Declaration and initialization.

    int i;

    for (i=0;iprintf("");

    printf("%s",a);Hit asdfadfprintf("%s",a+2);Hit dfadf

  2. Anonymous users2024-02-05

    to"\0"(part in quotation marks) as the end of the string. All characters before 0 will be output. And you don't have a 0 defined in your array, so the system doesn't know when to end the output.

    Just a few random values are output (this definition may not be scientific, because the system actually continues to output along the memory address of the string, which is actually beyond the memory address of the character array).

    So just add an element 0 to the end of the string array.

    In addition, the standard C++ main() function must be int rather than void, and must have a return value. void main() can only compile on some of Microsoft's compilers, and an error will occur with a different compiler. So it is recommended to change void main() to int main() and add return 0;

    The modified procedure is as follows:

    #include

    using namespace std;

    int main()

    char str[6]=;

    cout If it is helpful to you, please remember to answer for satisfaction, thank you! Have a great day!

  3. Anonymous users2024-02-04

    char *s="abcde", which will be added automatically'\0', but char s[8]="abcdabcd", it's exactly 8 squares, and there isn't'\0'Finish.

  4. Anonymous users2024-02-03

    Programming Basics - Character Arrays and Strings.

  5. Anonymous users2024-02-02

    An array of characters is defined as follows:

    #include

    using namespace std;

    int main(), which defines the content.

    Output: hello

    Happy learning!

  6. Anonymous users2024-02-01

    1. The end sign is different.

    String: Required'\0'End; The meaning of 0 ' is "string terminator".

    Character array: Can contain more than one'\0', but if it is treated as a string, the actual valid string is the first'\0'If the signature string is treated as an array of characters, it can handle any one character of the character array, and all characters can be'\0'。

    2. The characteristics are different.

    A string is stored like an array of characters, so a single element of each of its bits can be extracted, e.g. s="abcdefghij", then s[1]="b", s[9]="j"。

    And the zero bit of a string is exactly its length, as in s[0]=10 ( The above function ansistring does not. This can provide us with a lot of convenience, such as high-precision operations where each bit can be converted into numbers and stored in arrays.

    An array of characters is an array used to store data of characters. The general form of its definition is: char array name [data length].

    An array of characters is used to hold characters or strings, and an element in an array of characters holds a character, which occupies one byte in memory. There are no string types in C, strings are stored in an array of characters.

  7. Anonymous users2024-01-31

    Strings and character arrays are two different types of data, and they have many different characteristics.

    A string is a special sequence of characters of the monostate spike species, which usually consists of a set of characters. A string is an object that supports many sail operations and methods, such as compare, find, replace, join, split, and so on. In high-level languages, a string is an immutable object whose contents cannot be changed once created.

    A character array is a set of stored characters, which consists of a single set of characters. The character array can modify a character or a certain segment of characters at will, and supports low-level operations.

    In summary, strings are simpler and more convenient to work with, while character arrays are more flexible in some cases.

  8. Anonymous users2024-01-30

    C is an abstract general-purpose programming language.

    It is widely used in life, then character arrays and strings.

    What's the difference? Here's an explanation of the problem:

    Product name: Dell

    Product model: Dell Lingna Tease Yue 5000

    System version: Windows 10

    Software version: Difference 1: The definition is different.

    1. The string pointer variable itself is a variable used to store the first address of the string. The string itself is stored in a contiguous piece of inner storage space, starting with the first address, and ends with '?As the end of the string.

    2. The character array is composed of several array elements, which can be used to store the entire string.

    Difference 2: The method of pointer to strings is different.

    1、char*ps=”clanguage”;can be written as char*ps; ps=”clanguage”;

    2. Array charst=cannot be written as charst[20];

    st=;You can only assign values to each element of a character array one by one.

    The above are the specific operation steps for this problem, I hope it can help friends in need.

  9. Anonymous users2024-01-29

    In C, a string is made up of an array of characters and a terminator. Usually with a blank letter missing character ('\0') to indicate the end of the string. Thus, in C, there is a distinction between a string and an array of characters, a string is a sequence of characters consisting of an array of characters and a terminator, while an array of characters is an array of characters.

    For example, here's an array of characters:

    char arr[5] =

    This is a string:

    char str hello";

    These two examples may seem similar, but in reality they are different. The character array has five elements, which are:'h','e','l','l'with'o', but there is no terminator. A string has six elements, namely:'h','e','l','l','o'with'\0', where the last meta slippery pants rotten element is the ending character.

  10. Anonymous users2024-01-28

    Agree with the above netizens said that the way of dealing with it is different, let me improve it.

    1. Strings.

    abc"This is a string constant (also called a string literal).

    The above example is a string that looks like it has 3 characters, but it actually has 4 characters, including the one after C'\0'The character is also a null character. The compiler ends processing when it encounters a null character.

    In programming, you can also put strings into string arrays:

    char str1 abc";The compiler automatically calculates the length of the string.

    char str2[20] =abc";This is an array of strings.

    Something enclosed in double quotes is considered a pointer to where the string is stored, similar to having an array name as a pointer to the location of the array.

    You can also initialize an array of strings like this:

    char str3 [4] = the length of the array of strings should be 1 more than the length of the string in order to accommodate it'\0'

    Note: There is no null character at the end, this is not a string, but an array of characters, and all elements that are not initialized are automatically initialized as null characters'\0'。

    Don't initialize the string array like this:

    char dont

    This way the compiler will not know to stop from ** until it finds the nearest null character in memory, fortunately there are many empty characters in memory, and it won't take too long.

    2. Array of characters.

    The biggest difference between the character array and the string array is that the processing range is different, the string array ends when it encounters a null character, and the character array can control the length of the processing according to the needs of the programmer. But as with string arrays, the last character of the character array must also be null, and the following initialization cannot be compiled:

    char str4 [1] = Error: Too many default values are set.

    When declaring an array (including an array of characters), the size of the array must be an evaluable integer. Before C99 adds variable-length arrays, the size of the array must be an integer constant, including an expression consisting of an integer constant.

    int n = 8;

    char str5[2*sizeof(int)+1];Effective.

    char str6[n];c99 after the valid variable-length array.

    Without crossing bounds, developers can programmatically handle character arrays as needed.

    Unlike processing strings, the Higi translator will continue to process the data after the null character when it encounters null characters.

    For example: char str7 [3] =

    str7[1] =b';

Related questions
5 answers2024-02-26

Add a field in Table 1 as a foreign key and Billno in Table 2 >>>More

7 answers2024-02-26

Do it by your train of thought.

Method 1. string strnumber="200m"; >>>More

7 answers2024-02-26

The string is immutable, and the list is mutable.

That is to say, once the meaning of the string is determined, it cannot be changed, but the list can. >>>More

6 answers2024-02-26

Use the ITOA function.

Prototype: extern char *itoa(int i); >>>More

11 answers2024-02-26

First of all, it is necessary to understand what a binary tree is (and I guess the subject also understands). >>>More