2
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?

6
  • 3
    I would recommend learning a bit about C/C++ because that's what you're writing in these unsafe blocks. I wouldn't really consider pointers part of C# and I doubt there's much about them in C# documentation. Commented Dec 12, 2012 at 0:42
  • The C# documentation is here. Commented Dec 12, 2012 at 0:46
  • I would avoid "unsafe" code as much as possible, only at parts where it is absolutely necessary to use it. If you don't know when that is, and actually, since you don't know anything about this; I feel like you should avoid this area still. (Nothing wrong though with looking at it.) Commented Dec 12, 2012 at 0:50
  • @mayhewr yeah, I would say that's insufficient explanation unless you already know about them from programming C/C++. Commented Dec 12, 2012 at 0:53
  • @evanmcdonnal you mean "int** p -- p is a pointer to pointer to an integer" isn't clear enough for a layman? ;) I figured it was at least a good place to start. Commented Dec 12, 2012 at 0:56

4 Answers 4

7

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).

Sign up to request clarification or add additional context in comments.

1 Comment

The above operations are very common in C – in modern C++ they're (almost) completely unnecessary. :-]
1

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.

Comments

1

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.