Autor Beitrag
M.Mü
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Sa 09.09.06 00:16 
Hallo, ich bin grad reichlich ratlos:

Der folgende Quellcode steht als vereinfachtes Beispiel für die Verwendung von Pointern in einem anderen Projekt.

In der Prozedur SetPointer wird ein String beschrieben und der Zeiger auf dessen Anfang gesetzt. Die Prozedur GetPointer soll beide auslesen und über zwei Labels darstellen. Das Problem: Der Compiler meint, dass "newb" der Prozedur Setpointer nirgends verwendet wird, und lässt die Zeile somit aus. Nur ist dem ja nicht so, weil der Zeiger in GetPointer ausgelesen wird. Was mache ich falsch?

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private-Deklarationen }
    A: string;
    B: ^string;
    procedure SetPointer(newb: Pointer; var newa: string);
    procedure GetPointer(na: string; nb: Pointer);
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetPointer(B, A);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  GetPointer(A, B);
end;


procedure TForm1.SetPointer(newb: Pointer; var newa: string);
begin
  newa := 'Hallo';
  newb := @newa;
end;


procedure TForm1.GetPointer(na: string; nb: Pointer);
begin
  Label1.Caption := 'na: ' + na;
  Label2.Caption := 'nb: ' + string(nb^);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  A := 'Pointer';
end;

end.



VG M.Mü

Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Sa 09.09.06 00:23 
Da fehlt ein var, daher kann er die Änderung nicht zurückgeben.

ausblenden Delphi-Quelltext
1:
SetPointer(varnewb: Pointer; var newa: string);					


PS:benutze bitte Delphi- statt Code-Tags. [ delphi]...[ /delphi]

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
Grenzgaenger
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 09.09.06 01:50 
ist ja nur ein hinweis und keine fehlermeldung.

jedoch frag ich mich beim ersten durchschaun des codes, wo hast du denn die variable definiert?

//edit: weiss zwar nicht was du machen willst, aber ohne fehlermeldung läuft dieser code:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private-Deklarationen }
    A: string;
    B: ^string;
    procedure SetPointer(newb: Pointer; var newa: string);
    procedure GetPointer(na: string; nb: Pointer);
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure tform1.SetPointer(newb: Pointer; var newa: string);
begin
  newa := 'Hallo';
  newb := @newa;
end;

procedure tform1.GetPointer(na: string; nb: Pointer);
begin
  Label1.Caption := 'na: ' + na;
  Label2.Caption := 'nb: ' + string(nb^);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 SetPointer(@B, A);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 GetPointer(A, @B);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
 A := 'Pointer';
end;

end.
M.Mü Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Sa 09.09.06 20:34 
Hallo,

@Grenzgaenger

Mit dem Quellcode habe ich das gleiche Ergebnis.

@Martok

Das hatte ich als erstes probiert, allerdings auch mit dem selben Ergebnis

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm1.Button1Click(Sender: TObject);
var
  A: string;
  B: Pointer;
begin
  SetPointer(A, B);
  Label2.Caption := string(B^);
end;

procedure TForm1.SetPointer(var newa: string; newb: Pointer);
begin
  newa := 'Hallo';
  newb := @newa;
  //Label1.Caption := string(newb^);
end;


Es geht eigentlich nur darum in einer Prozedur einen Pointer zu setzen, und diesen als Ergebnis zurück zugeben. Die Codeschnipsel waren nur zur Fehlersuche, doch als ich keinen fand landeten sie hier.

Kann das vielleicht ein CompilerBug sein? Ich nutze Delphi 6

VG M.Mü
Born-to-Frag
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: Sa 09.09.06 20:47 
Hi!

Versuchs mal so ;)

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private-Deklarationen }
    A: string;
    B: PString;
    procedure SetPointer(var newb: PString; var newa: string);
    procedure GetPointer(var na: string; var nb: PString);
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetPointer(B, A);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  GetPointer(A, B);
end;


procedure TForm1.SetPointer(var newb: PString; var newa: string);
begin
  newa := 'Hallo';
  newb := @newa;
end;


procedure TForm1.GetPointer(var na: string; var nb: PString);
begin
  Label1.Caption := 'na: ' + na;
  Label2.Caption := 'nb: ' + nb^;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  A := 'Pointer';
end;

end.
M.Mü Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Sa 09.09.06 20:55 
Nun klappts :-)

Sind PString und ^string unterschiedliche Geschichten?

VG M.Mü
Born-to-Frag
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: Sa 09.09.06 21:00 
Nein, die Deklaration von PString ist:

ausblenden Delphi-Quelltext
1:
2:
type
  PString = ^String;


nur kannst du im Prozedurkopf nicht schreiben:
ausblenden Delphi-Quelltext
1:
MeineProc(a: ^String);					


;)


greetz

_________________
Theorie ist wenn man alles weiß, aber nichts funktioniert. Praxis ist wenn alles funktioniert, aber niemand weiß warum.
Microsoft vereint Theorie und Praxis: Nichts funktioniert und niemand weiß warum.