Strings, Pointers, Arrays, and .Text

What is the actual difference between each of these data types and how are they used, and most important, how do I convert between each of them? Also, which type do I use for passing 'C' type strings to a dll?

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

String is not supported in C.
PChar is. PChar's are equivalent to char * in C. In other words, a null-terminated string.

if you have C function like this:

void SomeFunc(int A, int *B, char *C)

the equivalent in Delphi would be:

procedure SomeFunc(A: Integer; var B: Integer; C: PChar);

Why use the var you may ask? Well, in the C call, int *B is requesting that address of the integer B. It will also let you return a value from the procedure through B. In Delphi, a var parameter is equivalent. In most instances, Delphi will pass the actual value to a procedure...by using a var, you are forcing Delphi to pass the address of that value.

That is all a pointer is...an address reference to that actual value. If you have this Delphi syntax:

var
B: Integer;
C: ^Integer;
begin
B := 15;
C := @B;
end;

B holds an actual value. That value has to be stored in memory somewhere. C is an address in memory. It could be any size space, but when C is dereferenced, it will pull out a number of bytes, starting at its address, equal to the length of an Integer. The @ symbol means the address of the variable. Therefore C := @B, means that C now points to the address of B value, 15. If you dereferenced C, like this:

lblLabel1.Caption := IntToStr(C^);

the label would now show the value 15. C^, means go to my address and get me the a value based on the type of pointer I am, in this case a pointer to an integer.

As for converting...there are specific functions to convert between string and pchar. See the Delphi help file under String Manipulation Routines.

The .Text of most components is of type string, therefore you have to convert most other types to string before you can store a value in a .Text property. I showed you one above, where we converted an Integer to a String via the IntToStr function.

Hopefully this clears some things up. Feel free to send more comments.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.