473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pointer, is this ok?

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
Oct 19 '06 #1
4 1076
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

Oct 19 '06 #2
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.
Oct 19 '06 #3
"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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.
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
Oct 19 '06 #4
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
Oct 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ali | last post by:
Hi, I'm trying to understand the reason for different output on the following codes Code1: #include <iostream.h> int main()
9
by: iceColdFire | last post by:
HI, I have a function as void f(int p) { return p++; } now I have created a function pointer as
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
33
by: dough | last post by:
Is it possible in C to declare and initialize a pointer that points to itself? Why or why not?
1
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
6
by: lithiumcat | last post by:
Hi, maybe you remember me, some time ago I asked about how to store an integer value into a void*, and I learned that doing pointer arithmetic yeilding a pointer outside of an object (except the...
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.