fixed
prevents
o
from being moved. Because
p
points to
o.num
, if
o
were moved, then
p
would point to an invalid location.
Accessing Structure Members Through a Pointer
A pointer can point to an object of a structure type as long as the structure does not contain
reference types. When you access a member of a structure through a pointer, you must use
the arrow operator, which is
–>
, rather than the dot (
.
) operator. For example, given this
structure,
struct MyStruct {
public int a;
public int b;
public int Sum() { return a + b; }
}
you would access its members through a pointer, like this:
MyStruct o = new MyStruct();
MyStruct* p; // declare a pointer
p = &o;
p->a = 10; // use the -> operator
p->b = 20; // use the -> operator
Console.WriteLine("Sum is " + p->Sum());
Do'stlaringiz bilan baham: |