为什么要用pchar来强制转换一个string类型的参数。。谁可以详细的叙述一下pchar的作用最好用例子来阐述。讲讲string ,pchar ,数组之间的区别

解决方案 »

  1.   

    下面是我的感觉不知道对不对:
    String类型是Delphi的一种比较特别的类型,处理字符串用。但是不是针对指针操作的。
    而Pchar 相当于C++的字符串数组,是有起始内存和结束内存的地址的,所以一般用C++写的
    DLL中Char * 参数在Delphi中使用时一般都用Pchar来代替,如果用String的话由于没有内存地址而容易出错!
      

  2.   

    在DELPHI中关于字符串函数一般都有两个版本如:计算长度的LENGTH,和STRLEN就是分别对应STRING和PCHAR的
      

  3.   

    pchar就是字符数组,它是为了调用API参数而用的,它以‘/0’结束的~~~~~
      

  4.   

    PChar是字符串指针,指向以#0结尾的字符串。
    string是Delphi中的一般类型(还有基础类型例如ShortString,AnsiString,WideString)
    类型        最大长度        占用内存     用于
     
    ShortString 255个字符       2到256字节   向后兼容
     
    AnsiString  大约2^31个字符  4字节到2GB   8位(ANSI)字符
     
    WideString  大约2^30个字符  4字节到2GB   多用户服务和多语言应用程序
     
    Delphi推荐使用一般类型。因为string是以#0结尾所以和PChar兼容。
      

  5.   

    那请看这个函数声名:procedure getDomain(DomainName:string;s:string);
    procedure tform1.getDomain(DomainName:string;s:string);
    var n,j:integer;
        sbuf:array[0..512] Of char;
        stmp:array[0..80] Of char;
        sFlag:pchar;
        str:string;
        f:textfile; // 文本文件类型
        i:Integer;
    begin
        AssignFile(f,'ftpdini.ini');
        Reset(f);
        n:=0;
        while not Eof(f) do
         begin
            Readln(f, str);
            strpcopy(sbuf,str);
            for i:=0 to 80 do
            begin
                stmp[i]:=sbuf[i];
                if  sbuf[i]='=' then
                begin
                    stmp[i]:=#0;
                    n:=i+1;
                    break;
                end;
            end;
            if strcomp(PChar(domainname),Pchar('UPIP'))=0 then
            begin
            if strcomp(stmp,'UPIP')=0 then
            begin
                 for i:=n to 80 do
                 begin
                    if  sbuf[i]=';' then
                        n:=i+1;
                 end;
                 j:=0;
                 for i:=n to 80 do
                 begin
                    stmp[j]:=sbuf[i];
                    if  sbuf[i]=#0 then
                        break;
                    j:=j+1;
                 end;
                 strcopy(Pchar(s),stmp);
            end;
            end;
            if strcomp(PChar(domainname),pchar('HOSTIP'))=0 then
            begin
            if strcomp(stmp,'HOSTIP')=0 then
            begin
                 j:=0;
                 for i:=n to 80 do
                 begin
                    stmp[j]:=sbuf[i];
                    if  sbuf[i]=';' then
                    begin
                       stmp[j]:=#0;
                       break;
                    end;
                    j:=j+1;
                 end;
                 strcopy(Pchar(s),stmp);
            end;
            end;
            if strcomp(Pchar(domainname),pchar('LOCALIP'))=0 then
            begin
            if strcomp(stmp,'LOCALIP')=0 then
            begin
                 for i:=n to 80 do
                 begin
                    if  sbuf[i]=';' then
                        n:=i+1;
                 end;
                 j:=0;
                 for i:=n to 80 do
                 begin
                    stmp[j]:=sbuf[i];
                    if  sbuf[i]=#0 then
                        break;
                    j:=j+1;
                 end;
                 strcopy(Pchar(s),stmp);
            end;
            end;
         end;
            CloseFile(f); // 关闭文件
    end;
    使用是:getDomain('HOSTIP',s1);
    编译没有问题?但使用它的时候就要内参溢出,这是为什么?
      

  6.   

    不对呀,我记得string的使用是动态分配内存的,PChar是指针的,都是动态的,可是他们就是不一样,为什么??????????
      

  7.   

    PChar是指向字符的指针,而string是指向字符串地址的指针。都是动态分配内存,不过还是有区别,总体上看基本上没有什么区别