Pointer Arithmetic in VC.net

G

Guest

Hi all,
I am porting my code in VC++ to VC.net i.e managed. I have seen somewhere
that i need to convert my char* to String*, but String * doesnt perform
pointer arithmetic like String* p; *p++=' ';
So please suggest as to what i have to do? Am i doing wrong by converting
char* to String* in managed VC.net.
Thanks,
Dipesh.
 
B

Ben Voigt [C++ MVP]

Dipesh_Sharma said:
Hi all,
I am porting my code in VC++ to VC.net i.e managed. I have seen somewhere
that i need to convert my char* to String*, but String * doesnt perform
pointer arithmetic like String* p; *p++=' ';
So please suggest as to what i have to do? Am i doing wrong by converting
char* to String* in managed VC.net.
Thanks,
Dipesh.

Which version of VC? C++/CLI (2005) handles this very differently from
2002, 2003. If you're not using 2005, upgrade now because of design bugs in
Managed Extensions for C++.
 
G

Guest

Hi Ben,
I am working on VS 2005, my problem is that : i am porting my code from VC++
to VC.net to make it .Net compatible. And i have learnt that char* are not
used in .Net instead we use String*. But my code is having extensive use of
char* in function calls, pointer arithmetic etc. So please suggest me as to
how i should make this change of char* to String* : in pointer arithmetic
i.e char *p++; and in function caling..
Please help me out..
Thanks.
 
B

Ben Voigt [C++ MVP]

Dipesh_Sharma said:
Hi Ben,
I am working on VS 2005, my problem is that : i am porting my code from
VC++
to VC.net to make it .Net compatible. And i have learnt that char* are not
used in .Net instead we use String*. But my code is having extensive use
of
char* in function calls, pointer arithmetic etc. So please suggest me as
to
how i should make this change of char* to String* : in pointer arithmetic
i.e char *p++; and in function caling..
Please help me out..
Thanks.

Don't. "String*" doesn't exist, you cannot have a native pointer to a
garbage collected object.

To get a pointer to the characters inside the String:

String^ s;

interior_ptr<const wchar_t> p = PtrToStringChars(s);

(Note that .NET strings are immutable, you are not permitted to overwrite
them in place, also they are Unicode).

If you need to convert to ASCII (single byte character sequences), there's
Marshal::ptrToStringAnsi. Also the String constructor accepts a character
pointer, so you can do

wchar_t* p = L"My native Unicode string";
String^ s = gcnew String(p);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top