Tutorial :Pointer increment and chaining precedence in C#



Question:

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   


Solution:1

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++;  


Solution:2

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.


Solution:3

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.


Solution:4

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.


Solution:5

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.   


Solution:6

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »