tags:

views:

137

answers:

6

I saw this piece of C# code in one of the msdn articles:

using System; class Test
{
   public static unsafe void Main() 
   {
      int* fib = stackalloc int[100];
      int* p = fib;
      *p++ = *p++ = 1;
      for (int i=2; i<100; ++i, ++p)
         *p = p[-1] + p[-2];
      for (int i=0; i<10; ++i)
         Console.WriteLine (fib[i]);
   }
}

I am fairly new to pointers. I understand most of this code, but it would be great if someone can help me understand this line in the above code in more detail:

*p++ = *p++ = 1 

Got my answer guys.. thanks a lot for the quick responses!

+8  A: 

That's just a lazy (others would say idiomatic) way to write

*p++ = 1;
*p++ = *p++;

or, perhaps better to understand:

*p=1;
p++;
*p=1;
p++;
Doc Brown
Okay! So *p=1, then the pointer moves to the next memory location. Then value at that location is set to 1 and then move to next location. Great!Thanks mate!
Raze2dust
If you find my answer useful, don't forget to accept it, please.
Doc Brown
yeah.. was just waiting for the 10 mins ;)
Raze2dust
+2  A: 

In C# *p++ = *p++ = 1; is equivalent to:

 *p++ = 1;
 *p++ = 1;

So the first 2 elements of the array (which is what p pointed to originally) are initialized to 1, and p is left pointing to the third element (element 2 using zero-based notation).

As an aside, note that a similar statement in C/C++ would have undefined behavior since the pointer p is modified more than once without an intervening 'sequence point'. However, C# evaluates the expression in a well defined manner.

Michael Burr
yeah.. I wish they hadn't complicated c# so much.. kinda confusing for beginners..
Raze2dust
raze2dust: If it makes you feel any better, use of pointers is entirely non-idiomatic C#. I've never written (or even seen) production code that uses pointers.
Gabe
+1  A: 

The first three fib numbers are 0, 1, 1 and that is what the code does. (Zero is skipped here)

It would be more clear to write:

*p++=1; // second fib
*p++=1; // third fib. 
InsertNickHere
The first number is not zero in the output of program.
MartyIX
Then its not the correct fibonacci sequence. As I said, its skipped here.
InsertNickHere
I'm sorry, I overlooked the note about zero. Sorry.
MartyIX
No problem. I just wonder why I don't get votes since my answer is quite near to the others (and answers the question imho)
InsertNickHere
+2  A: 

You need to break things down:

*p++

This is doing two things: dereference p and a post increment. I.e. show what is at the address p now (or assign to it) and after increment it (to the next memory location).

So two of these are allowing assignment to the initial and second memory locations (while leaving p referring to the third).

C# allows chained assignments: a = b = 2 assigns 2 to both a and b.

NB don't try this in C or C++, modifying the same thing (p) more than once in a single expression is undefined. But C# does define this.

Richard
+1 for the note about C/C++
MartyIX
+2  A: 

It means that at 0 and 1 positions of allocated memory values set to 1.

For more clear understanding: p* points to some adress of memory, let it be start, right to the another adress of memory, which equals start + sizeof(int)*100, let it will be end.

In this example to 1 were set elements at start and start + sizeof(int). Post increment (*p++) means that at first we use p* and then it increments with sizeof(int) value, the second *p++ means that we now use *p + sizeof(int) and then it increments once more and we have *p + sizeof(int)*2.

Eugene Cheverda
+1  A: 

In above code you are generating 100 numbers in Fibonacci series.

on line *p++ = *p++ = 1 , You are initializing first 2 numbers to 1. i.e. filling number 1 in places pointed by pointer.

Initially pointer is pointing to 0. When you do *p++ this means pointer should be moved ahead one place from where ever its pointing to.

So in this case value at position 1 is assigned to value at poisition 2 which is assigned to value "1". i.e. *p++ = *p++ = 1

YoK