Pointer Issues why can't I do this!

B

bozzzza

Consider:

struct myStructA
{
public unsafe fixed byte pA[10];
}

struct myStructB
{
public unsafe fixed byte pB[10];
}

unsafe class test
{
myStructA* A;
myStructB* B;

public void movePointer()
{
A->pA = B->pB;
}
}

The Compiler informs me The left-hand side of an assignment must be a
variable, property or indexer

Of course I could access the array using array notation, but that
would be longer than using pointer notation.

I have tried the same sort of thing using native C++ and that lets me
do it. Is there away I can get around this issue?
 
L

Lee

There is a deeper issue with your code.

If you replace A->pA = B->pB
with a simple assignment: var X = B->pB

Then if you place a breakpoint on this line, you will find that "*B =
Cannot dereference 'B'. The pointer is not valid." and that its
address is 0x00000000

L. Lee Saunders
http://oldschooldotnet.blogspot.com
 
G

Göran Andersson

bozzzza said:
Consider:

struct myStructA
{
public unsafe fixed byte pA[10];
}

struct myStructB
{
public unsafe fixed byte pB[10];
}

unsafe class test
{
myStructA* A;
myStructB* B;

public void movePointer()
{
A->pA = B->pB;
}
}

The Compiler informs me The left-hand side of an assignment must be a
variable, property or indexer

Of course I could access the array using array notation, but that
would be longer than using pointer notation.

I have tried the same sort of thing using native C++ and that lets me
do it. Is there away I can get around this issue?

They are not an arrays, they are fixed size buffers. Are you trying to
use arrays?
 
B

bozzzza

Not trying to use arrays, using fixed size buffers, my point was I can
access the buffer using array notation as a work around to the
problem.

I have re-written my last example to there is something to de-
reference, hopefully it's a bit clearer of what I mean:

struct myStructA
{
public unsafe fixed byte pA[10];
}

public unsafe class test
{
public void movePointer()
{
myStructA* A;

fixed (byte* y = new byte[10])
{
y[0] = 1;

//Example 1
//Does not work, (The left-hand side of an assignment
must be a variable, property or indexer message)
A->pA = y;

//Example2 works fine, but I really want the address
of the fixed size buffer to be stored in the Struct
byte* x = y; //Works ok

//Example3
//Works ok if using array notation on the fixed size
buffer, but that would mean iterating through the buffer.
//Rather than just assigning an address
A->pA[0] = y[0];
}
}
}
 
G

Göran Andersson

bozzzza said:
Not trying to use arrays, using fixed size buffers, my point was I can
access the buffer using array notation as a work around to the
problem.

I have re-written my last example to there is something to de-
reference, hopefully it's a bit clearer of what I mean:

struct myStructA
{
public unsafe fixed byte pA[10];
}

public unsafe class test
{
public void movePointer()
{
myStructA* A;

fixed (byte* y = new byte[10])
{
y[0] = 1;

//Example 1
//Does not work, (The left-hand side of an assignment
must be a variable, property or indexer message)
A->pA = y;

//Example2 works fine, but I really want the address
of the fixed size buffer to be stored in the Struct
byte* x = y; //Works ok

//Example3
//Works ok if using array notation on the fixed size
buffer, but that would mean iterating through the buffer.
//Rather than just assigning an address
A->pA[0] = y[0];
}
}
}

You can't store an address in the structure as there is nowhere to store
any address. The structure only contains the fized size buffer, and
there is no pointer to it.

If you want to put some bytes in the structure, you have to put them
bytes there one by one. Perhaps the Array.Copy can be used to copy the
bytes, but that will also do a byte by byte copy.
 

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