- // String and character array
- Var
- Arr: array [0 .. 5] of Char;
- Str: string;
- Begin
- {The String constant can be directly assigned to the character array, but not the character array}
- Arr: = 'delphi ';
- ShowMessage (arr); {Delphi}
- {Character Arrays can be directly assigned to string variables}
- Str: = arr;
- ShowMessage (str); {Delphi}
- {But cannot assign a string variable to the character array}
- // Arr: = str; {error}
- {In fact, the string also contains an array of characters, so it can be accessed by indexing, but its index starts at 1}
- ShowMessage (str [1]); {D}
- ShowMessage (arr [0]); {D}
- End;
- ------------------------------------------------------------------------------ // Character pointer and character array
- Var
- Arr: array [0 .. 6] of Char;
- P: PChar;
- Begin
- Arr: = 'delphi ';
- {If the character array is directly given to the character pointer, the result is not insurance, because the character pointer should end with a blank character (#0}
- {Add the last element of the array to #0}
- Arr [Length (arr)-1]: = #0;
- P: = arr;
- ShowMessage (p); {Delphi}
- {What If I give #0 to the center ?}
- Arr [3]: = #0;
- P: = arr;
- ShowMessage (p); {Del; truncated}
- End;