简体   繁体   English

如何为 Char 数组赋值?

[英]How can I assign a value to a Char array?

Say I have the variable:说我有变量:

Var question : array[1..50] of char;

When I do:当我做:

question := 't'; //What is the correct way to change the value?

It returns an error:它返回一个错误:

Incompatible types: 'array[1..50] of Char' and 'Char'不兼容的类型:'array[1..50] of Char' 和 'Char'

Note: I want to have a max string size of 50 chars, not 50 different chars.注意:我希望最大字符串大小为 50 个字符,而不是 50 个不同的字符。

The reason for this question is that I have a record in another unit(This is just a basic example, not what I actually have written above) In that unit I have a Record, which I can't use the string data type in(Or is there a way? please explain if there is).这个问题的原因是我在另一个单元中有一条记录(这只是一个基本示例,不是我上面实际写的)在那个单元中我有一个记录,我不能在其中使用字符串数据类型(或者有什么办法吗?如果有请说明)。 I just need to know how to give an array of chars a value.我只需要知道如何给一个字符数组赋值。

While Delphi strings and arrays of char are related, they are not the same.虽然 Delphi 字符串和 char 的 arrays 是相关的,但它们并不相同。

Delphi overloads assignment of strings and literals (char and string) to array of chars, but only when the lower array bound is zero. Delphi 重载将字符串和文字(字符和字符串)分配给字符数组,但仅当数组下限为零时。

The following code works for me in D2007 and Delphi XE:以下代码适用于 D2007 和 Delphi XE:

  var x : array[0..49] of char;
  begin
    x:='b';  // char literal
    x:='bb';  // string literal      
  end.

If I change the [0 to [1 it fails.如果我将 [0 更改为 [1 它将失败。 This limitation probably simplifies the language helper that takes care of this, and probably the feature is only meant for dealing with converted C structs where arrays always have lower bound 0.此限制可能简化了处理此问题的语言助手,并且该功能可能仅用于处理转换后的 C 结构,其中 arrays 始终具有下限 0。

Are you sure that you can't use string data type in a record?您确定不能在记录中使用字符串数据类型吗? Anyways...无论如何...

type
  TCharArray = array[Char] of Char;

function StringToArray(Str: string): TCharArray;
begin
  FillChar(Result, High(Byte), #0);
  Move(Str[1], Result, length(Str));
end;

procedure TestCharArray;
var
  question: TCharArray;
begin
  question := StringToArray('123');
  ShowMessage(PChar(@question));
end;

Also take a look at StrPCopy function.另请查看StrPCopy function。

If you don't need unicode characters, you should just define your string like string[50] .如果您不需要 unicode 字符,您应该只定义您的字符串,如string[50]

After that you don't need any functions or conversions to work with that string, and it'll be just as easy to read and write it to a file.之后,您不需要任何函数或转换来处理该字符串,并且将其读取和写入文件同样容易。

Hscores = record
var
  _topscore : integer;
  _topname : string[50];
end;  

I'm pretty sure you can use strings in record types.我很确定您可以在记录类型中使用字符串。

This blog entry shows an example: http://delphi.about.com/od/beginners/a/record_type.htm此博客条目显示了一个示例: http://delphi.about.com/od/beginners/a/record_type.htm

In order to assign a value to the Char array, you have to index it, like any other array:为了给 Char 数组赋值,你必须像其他数组一样对其进行索引:

question[1] := 't';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM