Reputation: 461
class f2011fq1d
{
unsafe public static void Main()
{
int a = 2;
int b = 4;
int* p;
int* q;
int[] ia = { 11, 12, 13 };
p = &a; q = &b;
Console.WriteLine(*p + a);
Console.WriteLine(*q / *p);
Console.WriteLine(*&a + *&b * 2);
*p = a + *q;
Console.WriteLine(a + *q);
fixed (int* r = ia)
{
Console.WriteLine(*r + 3);
}
}
}
In this code, I'm confused about some of the syntax. For example, what does int* p
, and p = &a
do? And the last part with the fixed (int* r = ia)
, what does that do?
In the source code, it also prints a few values, can someone explain what is being printed?
Upvotes: 2
Views: 408
Reputation: 48124
You're using pointers and the 'address of operator' these are features commonly associated with C/C++ but are available in C# code within unsafe
code blocks. I would recommend doing some general research on the concept because understanding the syntax alone will not be enough to effectively use them. I'll explain a few lines;
int * p; //a pointer (the address of) 4 bytes of memory for an int
int a = 5; // normal allocation/initialization of int
p = &a; //sets p to the address of a. Since p is a pointer, it holds an address
// ampersand is the 'address of operator', it returns and address. so this assignment works
p = a // will not compile. int* != int
p == 5 // will not compile, int* != int
*p == 5 // true. *pointer dereferences the pointer returning the value
// found at the address p points to which is 5 so this is true.
int * q = &a; // another pointer to the memory containing a
p == q // true, both pointers contain the same value, some address which is usually displayed in hex like 0xFF110AB2
A more common use of pointers would be something like;
int *p;
// do something to get a length for an integer array at runtime
p = new int[size];
// now I've allocated an array based on information found at run time!
The above operations are very common in C/C++ and completely necessary. In C# there's no point in doing this. It's being done for you under the covers already, and it will never make mistakes (like failing to free that memory I just allocated).
Upvotes: 7
Reputation: 64248
In this code, I'm confused about some of the syntax. For example...
I'll try and address these one at a time ( no pun intended )
what does int* p
This declares a local variable called 'p' who's type is a pointer to an int. Thus when this variable is assigned to a memory address it will read 4-bytes as if they were an int32.
... and p = &a
The '&a' on the right is read as "the address of a", meaning take the memory location we assigned to the local variable 'a'. Assigning this to the already declared int pointer, 'p', we can then read 'p' to get the value actually stored in the variable 'a'.
And the last part with the fixed (int* r = ia), what does that do?
This is very similar to the assignment of p to the address of a except there is not an "the address of" prefix. This is because the type of the variable 'ia' is an array which is being treated implicitly as if it were a pointer. Thus this statement declares an int pointer 'r' and assigns it to the address of the first item in the array 'ia'.
Here are a few more notes:
if (*p == 2) ...
A pointer prefixed with the '*' will dereference that pointer and be treated as if it were a variable of the type of pointer. Thus in our example dereferencing 'p' who points to the variable 'a' will result in the value stored in 'a', 2.
*p = 100;
Just like reading a value at the address pointed to by 'p', we can also write a new value. Simply dereference the pointer and assign it a new value.
*&a
This is just intended to confuse someone. As explained it takes the address of 'a' by prefixing '&', but then dereferences the address with the '*' prefix. This simply evaluates to 'a' and both prefixes can be omitted without a change in behavior.
Upvotes: 1
Reputation: 35477
With unsafe
code, C# allows you to use pointers and addresses such like C. int *p
declares a pointer to an int
. p = &a
, takes the address of a
and stores it in p
.
fixed
prevents the garbage collector from moving the data.
See http://msdn.microsoft.com/en-us/library/chfa2zb8(v=vs.100).aspx and http://msdn.microsoft.com/en-us/library/f58wzh21(v=vs.100).aspx
Upvotes: 1
Reputation: 14157
int* p
declares a variable named p
that is a pointer to an int.
p = &a
causes p
to point to the same location in memory as a
.
Pointer types (C# Programming Guide) is probably a good place to start if you need to understand this kind of stuff.
Upvotes: 1