tags:

views:

196

answers:

4

Im new to pointers (just began learning) and came across the following code snippet and needed to predict the output. My answer was 220 but was told its wrong. Could someone tell me the correct output and please explain why.

using System;
class pointer
{
  public static void Main()
  {
   int ptr1=0;
   int* ptr2=&ptr1;
   *ptr2=220;
   Console.WriteLine(ptr1);
  }
}

EDIT: Thank you everybody for the explanatory answers. The correct answer is definitely 220 if and only if the above block of code (which is C# code, Sorry for not mentioning it in the question) was declared as unmanaged. Thank you for all your answers. Every one of em was really informative and helpful.

+6  A: 

The answer is that it does not compile. You will get the following error:

error CS0214: Pointers and fixed size buffers may only be used in an unsafe contex

If, however, you write this:

int ptr1 = 0;

unsafe {
 int* ptr2 = &ptr1;
 *ptr2 = 220;
}

Console.WriteLine(ptr1);

Then you will indeed get 220.

You can also create an entire unsafe method, rather than creating specific unsafe blocks:

public unsafe void Something() {
 /* pointer manipulation */
}

Note: you also have to compile with the /unsafe switch (check "Allow unsafe code" in the project properties in Visual Studio)

Edit: Have a look at Pointer fun with binky for a short, funny, yet informative video on pointers.

IRBMe
+5  A: 

The result is 220, below is a C# code snippet to test it (don't have C++ here)

using System;

internal class Pointer {
    public unsafe void Main() {
     int ptr1 = 0;
     int* ptr2 = &ptr1;
     *ptr2 = 220;

     Console.WriteLine(ptr1);
    }
}

The steps:

  • PTR1 is assigned value 0
  • PTR2 is pointing to the address space of PTR1
  • PTR2 is assigned value 220 (but is pointing to the address space of PTR1)
  • So when PTR1 is now requested the value is also 220.

Ask your teacher to get me an A as well ;)

Zyphrax
Well, line 3 should be "The integer at the location PTR2 points to is assigned value 220". PTR2's value (the memory location of PTR1) has not changed.
darron
@Zyphrax: I copied your "The Steps" section into some documentation for later use. Thanks. :)
Zack
+3  A: 

I don't know anything about pointers in C#, but I can try to explain what it does in C/C++:

public static void Main()
{
  // The variable name is misleading, because it is an integer, not a pointer
  int ptr1 = 0;

  // This is a pointer, and it is initialised with the address of ptr1 (@ptr1)
  // so it points to prt1.
  int* ptr2 = &ptr1;

  // This assigns to the int variable that ptr2 points to (*ptr2,
  // it now points to ptr1) the value of 220
  *ptr2 = 220;

  // Therefore ptr1 is now 220, the following line should write '220' to the console
  Console.WriteLine(ptr1);
}
Treb
+1  A: 

@Zaki: You need to mark your assembly to allow unsafe code and block out your unsafe code like so:

public static class Program {
 [STAThread()]
 public static void Main(params String[] args) {
  Int32 x = 2;
  unsafe {
   Int32* ptr = &x;
   Console.WriteLine("pointer: {0}", *ptr);

   *ptr = 400;
   Console.WriteLine("pointer (new value): {0}", *ptr);
  }
  Console.WriteLine("non-pointer: " + x);

  Console.ReadKey(true);
 }
}

To be honest, I've never used pointers in C# (never had a use).

I did a quick Google Search and found this which helped me produce the above example.

Zack
@Zack: Thanks a lot, very informative link.
Zaki
no problem :) I learned something new, so I'm glad to share it. Hoping someone could maybe eval. my code a bit and lemme know what they think of it. :)
Zack