Use fixed to get a pointer to the start of a string : Pointer Unsafe « Language Basics « C# / C Sharp






Use fixed to get a pointer to the start of a string

Use fixed to get a pointer to the start of a string
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Use fixed to get a pointer to the start of a string. 
 
using System; 
 
public class FixedString { 
  unsafe public static void Main() { 
    string str = "this is a test"; 
 
    // Point p to start of str. 
    fixed(char* p = str) { 
 
      // Display the contents of str via p. 
      for(int i=0; p[i] != 0; i++) 
        Console.Write(p[i]); 
    } 
 
    Console.WriteLine(); 
     
  } 
}


           
       








Related examples in the same category

1.References and Pointers
2.Demonstrate pointers and unsafeDemonstrate pointers and unsafe
3.Demonstrate fixedDemonstrate fixed
4.Demonstrate the effects of pointer arithmethicDemonstrate the effects of pointer arithmethic
5.Demonstrate pointer comparisonDemonstrate pointer comparison
6.An array name with an index yields a pointer to the start of the arrayAn array name with an index yields a pointer to the 
   start of the array
7.Index a pointer as if it were an arrayIndex a pointer as if it were an array
8.Multiple IndirectMultiple Indirect
9.Unsafe code: copyUnsafe code: copy
10.Pointers and Declarative Pinning
11.Unsafe ContextUnsafe Context