ObjectSharp Blogs

You are currently viewing

Dave Lloyd's 2 Cents

A .NET Developer's Perspective


So you think you know Pointers?

This might be a bit of fun for the old C programmers out there like me.

A long time ago I used to teach C programming. This was before C++ and Windows. When we used to have to write whole applications that fit into 250K of Ram because the rest of the 640K was used up by various hardware drivers, and DOS, and you had to use tools like Quarterdeck QEMM to move drivers into memory locations above 640K. These were the days when hardware was expensive and developers had to really worry about memory usage and pointers and stacks..... Oh sorry I got a little carried away there, that is not what this Blog is about.

Where was I? Oh yeah I used to teach C, at the end of every test I used to have bonus questions for my students. I found one the other day and thought I would put it on my Blog for fun. This particular bonus question was meant to test their understanding of the pointer operators ( * & ), amoung other things.

Now to use pointers in C#, I had to make the code unsafe.  With todays compilers and editors, it would be easy to see what this little method does. So see if you can figure it out just by looking at the code right here in my Blog.

Can you tell me what i will equal, and therefore what will be written to the console.

unsafe static void Foo()
{
      int i, k=5;     /* Declare some Integers */
      int *j;           /* Declare a pointer */

      j = &k;        /* Assign a Value */
      i = 10/*j;     /* Do the Math */;

      Console.WriteLine( i.ToString() );
}

If you are thinking this is too easy, and the answer is 2. Try again.

Comments

  • dave May 18, 2004 11:19 PM

    It is way too easy...10 I have seen this before and I think it was from you, way back when... :)

  • dave May 19, 2004 12:07 AM

    Not Fair Bos you have seen it before. Don't say why. It's not as easy as you say if you have never seen it before.

  • dave May 19, 2004 1:49 PM

    I know it isn't fair, and I wasn't going to put the answer in. Although I think when you told me the answer was 10, it still took me a while to figure it out.

  • dave May 19, 2004 10:40 PM

    I just compiled and ran this code - the answer IS 2.

  • dave May 20, 2004 7:18 AM

    John... All I can say is you didn't type it in exactly as you see it here. Look at it again but think like a parser.

  • dave May 30, 2004 5:05 PM

    The problem is blatently obvious if you paste it into Visual Studio. Just goes to show the difference today's IDEs make!

  • dave June 1, 2004 10:47 AM

    There's a dead give-away that I saw right away, that sort of backed me into the answer. I admit I wouldn't have caught it had I not seen the thing that was clearly out of place.

Leave a Comment

(required) 
(optional)
(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS