Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Pointer bug ???



Permlink Replies: 5 - Last Post: Oct 12, 2014 1:32 AM Last Post By: Rudy Velthuis (...
Attila Kovács

Posts: 6
Registered: 2/14/12
Pointer bug ???
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 10, 2014 3:11 AM
Why p=p^ ???????

program Project1;
var p: ^char;
c:char;

begin

c:='a';
p:=@c;

writeln(p);
writeln(p^);

if p=p^ then write('same');

readln;
end.
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Pointer bug ???
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 10, 2014 4:17 AM   in response to: Attila Kovács in response to: Attila Kovács
Attila Kovács wrote:

Why p=p^ ???????

program Project1;
var p: ^char;
c:char;

begin

c:='a';
p:=@c;

writeln(p);
writeln(p^);

if p=p^ then write('same');

Because you are comparing different types, a few conversions happen
behind the scenes. A PChar (^Char) is converted to a string, and the
char is converted to a string too, and only then the comparison is
done. Since the strings will be the same, you get the result you didn't
expect.

Note that the PChar type is often used to point to C-style strings,
i.e. a series of Chars terminated by a #0 Char. So you can even do:

p := 'Hello';

and it will set p to the first character of such a string.

More about these things here: "PChars: no strings attached",
http://rvelthuis.de/articles/articles-pchars.html

--
Rudy Velthuis http://www.rvelthuis.de

"It's not at all important to get it right the first time. It's
vitally important to get it right the last time."
-- Andrew Hunt and David Thomas

Attila Kovács

Posts: 6
Registered: 2/14/12
Re: Pointer bug ???
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 10, 2014 10:58 PM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Thanks your informative reply
Your website is very useful
Steve Thackery

Posts: 151
Registered: 4/29/06
Re: Pointer bug ???
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 11, 2014 2:34 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy Velthuis (TeamB) wrote:

More about these things here: "PChars: no strings attached",
http://rvelthuis.de/articles/articles-pchars.html

Great explanation and very helpful. Thanks, Rudy.

--
SteveT
Quentin Correll


Posts: 2,412
Registered: 12/1/99
Re: Pointer bug ???
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 11, 2014 10:18 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy,

| More about these things here: "PChars: no strings attached",
| http://rvelthuis.de/articles/articles-pchars.html

I don't know if anyone has ever thanked you for your articles. So I'll
say "Thank You!" now. I take a look every-now-and-then. <g> I have
noticed one or two that are in need of some updates but I do appreciate
the effort you have put into them!!!

--

Q

1.19.1.372 (Q's Broken Toolbar.)
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Pointer bug ???
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 12, 2014 1:32 AM   in response to: Quentin Correll in response to: Quentin Correll
Quentin Correll wrote:

Rudy,

More about these things here: "PChars: no strings attached",
http://rvelthuis.de/articles/articles-pchars.html

I don't know if anyone has ever thanked you for your articles. So
I'll say "Thank You!" now.

<sniffing, wiping eye> Thanks!

--
Rudy Velthuis http://www.rvelthuis.de

"There are perhaps 5% of the population that simply can't think.
There are another 5% who can, and do.
The remaining 90% can think, but don't." -- Robert A. Heinlein
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02