In c and in c++ a character array is an array of bytes, normally containing ASCII values. A literal string is an sequence of ASCII characters terminated with a null ('\0'). Example:
char array[9] = { 'a','b','c','d','e','f','g','h','i','j' };
char str[] = "abcdefghij";
char *ptr = "abcdefghij";
array[] will contain the ASCII string 'abcdefghij' without a null string terminator: example array[] contains:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
a b c d e f g h i j
str[] contains:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
a b c d e f g h i j '\0'
ptr will contain the address of the first byte of the literal string "abcdefghij"; ptr+10 will be the address of the null terminator.
In c++, what is the difference between a character array (or a pointer to one) and a literal string?
there is some difference in memory assignment and accesing if you put alignment larger than 1. Say compiler option alignment is 8, then compile will assign 8 byte space for one char. For string, it assignes memory space for each char either one byte (ANSI) or two bytes (unicode).
Therefore, accessing those char in array will be more complicated
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment