Autor Beitrag
timitio
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 09.04.05 14:40 
Hi Leute,

hab noch nicht so ganz den Durchblick bei nullterminierten Strings, PChars und array of chars usw.

Jedenfalls geht Folgendes nicht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
var
  s: string;
  a: array[0..63] of char;
begin
  s := 'Das ist ein String';
  a := s; //<== das geht halt nicht
end;


Gibt es eine andere Möglichkeit außer:

ausblenden Delphi-Quelltext
1:
2:
for i := 0 to length(s)-1 do
  a[i] := MidStr(s, i, 1);


Zuletzt bearbeitet von timitio am Sa 09.04.05 14:57, insgesamt 1-mal bearbeitet
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Sa 09.04.05 14:45 
Also da fällt mir jetzt auch nix ein, aber dein Code ist falsch, gell?

ausblenden Delphi-Quelltext
1:
2:
for i := 0 to length(s)-1 do
  a[i] := MidStr(s, i, 1);

s[0] gibbet nich... ;)
ausblenden Delphi-Quelltext
1:
2:
for i := 1 to length(s) do
  a[i] := MidStr(s, i, 1);

So... Und dann würd ich das noch so schreiben:
ausblenden Delphi-Quelltext
1:
2:
for i := 1 to length(s) do
  a[i] := s[i];

;)
timitio
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 09.04.05 14:48 
WeBsPaCe

ups, das mit der for-Schleife war aus dem Kopf.
Gibt es aber für das Problem einen einzigen Befehl?

Außerdem, kann noch im Nachhinein meinen tollen Titel ändern?
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Sa 09.04.05 14:52 
user profile icontimitio hat folgendes geschrieben:
WeBsPaCe

Was gegen meinen Nick??? :motz:

:mrgreen:

user profile icontimitio hat folgendes geschrieben:
ups, das mit der for-Schleife war aus dem Kopf.

Wie? :gruebel:

user profile icontimitio hat folgendes geschrieben:
Gibt es aber für das Problem einen einzigen Befehl?

Ich weiß keinen... ;)

user profile icontimitio hat folgendes geschrieben:
Außerdem, kann noch im Nachhinein meinen tollen Titel ändern?

Einfach bei deinem ersten Beitrag da oben auf Edit klicken und bei Titel was anderes reinschreiben... ;)
wdbee
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 628
Erhaltene Danke: 1



BeitragVerfasst: Sa 09.04.05 16:07 
Das ist eine Möglichkeit:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.Button1Click(Sender: TObject);
const
  n = 63;
var
  s: string;
  a: array[0..n] of char;
begin
  s := 'Das ist ein String';
  StrLCopy(a,PChar(s),Min(n,Length(s))); // Von s nach a kopieren, max. n Zeichen, a[n] bleibt = 0
  ShowMessage(a);
end;
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Sa 09.04.05 16:41 
Gut, aber ohne nachgesehen zu haben... Das wird genau das selbe machen... ;) Und ich kann auch einfach ne Funktion schreiben, die dann die beiden Parameter-Variablen gleichsetzt... ;)
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Sa 09.04.05 17:09 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
const n = 63;
var   
  s: string;   
  a: array[0..n] of char;
begin
  copymemory(@a,@s[1]length(s));
end;


Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt.

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
I.MacLeod
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 109



BeitragVerfasst: Sa 09.04.05 17:12 
@uall: Ungut: wenn length(s) > 64, knallts da ganz ordentlich.

_________________
{$APPTYPE CONSOLE}uses SysUtils;const a='{$APPTYPE CONSOLE}uses SysUtils;const a=%s;begin write(Format(a,[#39+a+#39]))end.';begin write(Format(a,[#39+a+#39]))end.
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Sa 09.04.05 17:18 
schon aber das fragt man ja vorher ab ;>
da ich aber lieber mit pointern arbeite mach ich fast alles mit CopyMemory :)

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
wdbee
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 628
Erhaltene Danke: 1



BeitragVerfasst: Sa 09.04.05 17:32 
user profile iconuall@ogc hat folgendes geschrieben:
schon aber das fragt man ja vorher ab ;>
da ich aber lieber mit pointern arbeite mach ich fast alles mit CopyMemory :)


ausblenden Delphi-Quelltext
1:
function StrLCopy(Dest: PChar; const Source: PChar; MaxLen: Cardinal): PChar; assembler;					


Da hast du deine geliebten Zeiger. Die Funktion ist in Assembler implementiert, dürfte also mit CopyMemory mithalten können, und die Typumwandlung in der Parameterliste macht Delphi für dich.

Viele weitere Routinen zum Umgang mit nullterminierten Strings finden sich in der Delphihilfe (mit Beispielen). :wink:
wdbee
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 628
Erhaltene Danke: 1



BeitragVerfasst: Sa 09.04.05 17:36 
user profile iconWeBsPaCe hat folgendes geschrieben:
Gut, aber ohne nachgesehen zu haben... Das wird genau das selbe machen... ;) Und ich kann auch einfach ne Funktion schreiben, die dann die beiden Parameter-Variablen gleichsetzt... ;)


Mach halt mal den Vergleich, welches Verfahren schneller ist und die Begrenzung auf die maximale Länge fehlt bei dir auch noch.
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Sa 09.04.05 18:15 
user profile icontimitio hat folgendes geschrieben:
hab noch nicht so ganz den Durchblick bei nullterminierten Strings, PChars und array of chars usw.

Jedenfalls geht Folgendes nicht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
var
  s: string;
  a: array[0..63] of char;
begin
  s := 'Das ist ein String';
  a := s; //<== das geht halt nicht
end;


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
var
  S: String;
  A: array[0..63] of Char;
begin

  S := 'Das ist ein String';
  StrLCopy(A, PChar(S), SizeOf(A) - 1);

end;

_________________
Ciao, Sprint.