Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pointer, is this ok?

0 views
Skip to first unread message

Michael C

unread,
Oct 18, 2006, 11:22:20 PM10/18/06
to
I've got a pointer to a byte and want to set the byte it points to to zero
as well as the three bytes after. I could do this

dstPtr[0] = 0;
dstPtr[1] = 0;
dstPtr[2] = 0;
dstPtr[3] = 0;

but can convert it to an int* first and just do

*(int*)dstPtr = 0;

is this correct and is it good practice? Is it the best way to do it?

Thanks,
Michael


Marc Gravell

unread,
Oct 19, 2006, 1:17:25 AM10/19/06
to
You may have the wrong ng; this is c#... try c/c++ groups. An IntPtr
doesn't lend itself to this usage, although as it happens
System.Runtime.InteropServices.Marshal.ReadIntPtr(System.IntPtr) might
do the trick.

Marc

Mattias Sjögren

unread,
Oct 19, 2006, 1:52:42 AM10/19/06
to
Marc,

>You may have the wrong ng; this is c#... try c/c++ groups.

C# supports pointers too, you know.

Nothing wrong with casting to an int* first IMO.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Michael C

unread,
Oct 19, 2006, 1:54:18 AM10/19/06
to
"Marc Gravell" <marc.g...@gmail.com> wrote in message
news:1161235045.7...@e3g2000cwe.googlegroups.com...

That's not true. C# can work with pointers no problem. If you've got an
IntPtr you can write code like this

byte* ptr = (byte*) MyIntPtr;
*ptr = 5;
ptr ++;
etc

Michael


Marc Gravell

unread,
Oct 19, 2006, 5:45:56 AM10/19/06
to
Yikes! I'm truly embarrassed to say that I had completely overlooked that
usage of pointers withing C#. Learn a new thing etc...

I guess I've just tried to avoid "unsafe" except where absolutely necessary,
limiting myself to the safe variations.

But yup, I'll take that on the chin. I was completely wrong.

Marc


0 new messages