What is the difference between a Character Array and a String?

后端 未结 10 1086
眼角桃花
眼角桃花 2020-12-10 11:49

Spending my time on high level languages it suddenly occurred to me that I did not know the difference between a Character Array and a String. I think they are the same thin

相关标签:
10条回答
  • 2020-12-10 12:28

    In C, a string is an array of characters terminated by a null character(\0) but

    In C++, a string is a class and we use its object and there is no null character at the end but an array of characters contains null character at the end.

    Also, we can use operators with the string object in C++.

    0 讨论(0)
  • 2020-12-10 12:35

    A string is the character array terminated by null character ‘\0’

    0 讨论(0)
  • 2020-12-10 12:44

    The answer to some extent depends on what language your talking about. In the .Net/C# world strings are immutable objects, whereas a char array you can add/change values easily in the array. Strings can be treated as char arrays in a read-only fashion, as you can iterate over the characters in a string.

    In the abstract I think the biggest difference is in how you want to work with them. Are you wanting to work with a chunk of text, say to show a message to an end user, or are you looking at a sequence of characters, doing some processing on the list? It's all rather subjective at a certain level.

    0 讨论(0)
  • 2020-12-10 12:44

    It depends on the language. In C-ish languages, they are pretty much synonomous. You could claim the difference is that "strings" have an implicit terminating nul, but that would be splitting hairs.

    Fortran is the other extreme. There character arrays and character strings are entirely different types, with different operations available for them.

    0 讨论(0)
提交回复
热议问题