String, character array, character pointer

Source: Internet
Author: User
 
 
  1. // String and character array
  2. Var
  3. Arr: array [0 .. 5] of Char;
  4. Str: string;
  5. Begin
  6. {The String constant can be directly assigned to the character array, but not the character array}
  7. Arr: = 'delphi ';
  8. ShowMessage (arr); {Delphi}
  9. {Character Arrays can be directly assigned to string variables}
  10. Str: = arr;
  11. ShowMessage (str); {Delphi}
  12. {But cannot assign a string variable to the character array}
  13. // Arr: = str; {error}
  14. {In fact, the string also contains an array of characters, so it can be accessed by indexing, but its index starts at 1}
  15. ShowMessage (str [1]); {D}
  16. ShowMessage (arr [0]); {D}
  17. End;
  18. ------------------------------------------------------------------------------ // Character pointer and character array
  19. Var
  20. Arr: array [0 .. 6] of Char;
  21. P: PChar;
  22. Begin
  23. Arr: = 'delphi ';
  24. {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}
  25. {Add the last element of the array to #0}
  26. Arr [Length (arr)-1]: = #0;
  27. P: = arr;
  28. ShowMessage (p); {Delphi}
  29. {What If I give #0 to the center ?}
  30. Arr [3]: = #0;
  31. P: = arr;
  32. ShowMessage (p); {Del; truncated}
  33. End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.