Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. pointer in C#

pointer in C#

Scheduled Pinned Locked Moved C#
csharpquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amitmohanty
    wrote on last edited by
    #1

    Can anyone tell me if there is something like pointer in C#. If I want to interchange two rows in a mtrix which is the most efficient way? In C, probabaly we can use, if we wan to elliminate the extensive use of the stoarage space. Regards, Amit

    J G 2 Replies Last reply
    0
    • A amitmohanty

      Can anyone tell me if there is something like pointer in C#. If I want to interchange two rows in a mtrix which is the most efficient way? In C, probabaly we can use, if we wan to elliminate the extensive use of the stoarage space. Regards, Amit

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      You can use regular old pointers in C#. You just have to do it in an unsafe context:

      unsafe static void WriteLocations()
      {
      int *p;
      int i;
      i = 10;
      p = &i;

      string addr;
      addr = int.Format((int) p, "X");
      //To Format the Pointer to a String

      Console.WriteLine(addr);
      Console.WriteLine(*p);

      *p = 333;
      Console.WriteLine(i);
      //Change the Value using Pointer

      i = *(&i) + 10;
      Console.WriteLine(i);

      }

      Also, instead of marking a class or function as static, you can mark portions of code as unsafe:

      void DoSomething() // safe function
      {
      unsafe // unsafe block of code in safe function
      {
      // use pointers here
      }
      }

      Also note that the C# compiler needs the /unsafe switch turned on in order to use unsafe code.

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango

      A 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        You can use regular old pointers in C#. You just have to do it in an unsafe context:

        unsafe static void WriteLocations()
        {
        int *p;
        int i;
        i = 10;
        p = &i;

        string addr;
        addr = int.Format((int) p, "X");
        //To Format the Pointer to a String

        Console.WriteLine(addr);
        Console.WriteLine(*p);

        *p = 333;
        Console.WriteLine(i);
        //Change the Value using Pointer

        i = *(&i) + 10;
        Console.WriteLine(i);

        }

        Also, instead of marking a class or function as static, you can mark portions of code as unsafe:

        void DoSomething() // safe function
        {
        unsafe // unsafe block of code in safe function
        {
        // use pointers here
        }
        }

        Also note that the C# compiler needs the /unsafe switch turned on in order to use unsafe code.

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango

        A Offline
        A Offline
        amitmohanty
        wrote on last edited by
        #3

        Thanks Judah! How can one function be unsafe? what is a unsafe code. regards, Amit

        J 1 Reply Last reply
        0
        • A amitmohanty

          Thanks Judah! How can one function be unsafe? what is a unsafe code. regards, Amit

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          Unsafe code is

          • code that cannot be verified as type-safe by the .NET Common Language Runtime (CLR).
          • is not Common Language Specification (CLS)-compliant, meaning that other .NET languages like VB.NET, J#, and so on are not required to implement nor are they guaranteed to consume such code, if it were declared as public and made available to other .NET dlls & executables.
          • requires a full trust security scenario in order to run, for instance, it must be installed/run from the target machine; if you try to use pointers in code run in a partial-trust scenario (e.g. a C# applet over the internet), the .NET CLR will throw a SecurityException because pointers require full access to the machine's memory, thus are potentially dangerous to the end user.
            For a function, class, or code block to be unsafe, just use the unsafe keyword:

          unsafe class MyClass
          {
          }

          unsafe void MyFunction()
          {
          }

          void MyFunctionWithUnsafeBlock
          {

          unsafe
          {
          }

          }

          Despite all these cautions, you're free to use pointers as much as you like if your application is a standalone app or dll, indeed, the .NET framework uses pointers internally quite often for performance gains and interop scenarios.

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango

          1 Reply Last reply
          0
          • A amitmohanty

            Can anyone tell me if there is something like pointer in C#. If I want to interchange two rows in a mtrix which is the most efficient way? In C, probabaly we can use, if we wan to elliminate the extensive use of the stoarage space. Regards, Amit

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            There is seldom any real need for pointers. Can't you use a reference instead? --- b { font-weight: normal; }

            A 1 Reply Last reply
            0
            • G Guffa

              There is seldom any real need for pointers. Can't you use a reference instead? --- b { font-weight: normal; }

              A Offline
              A Offline
              amitmohanty
              wrote on last edited by
              #6

              I really wanted to use something which can allow me to interchange position of two rows in a 2D array. say i want to make 3rd row 7th row and place 7th row as the third row. I don't want to use a temporary 1Darray for that. If each row has say 1000 elements, then i need 1000 temporary varibale to do the calculation. I don't want to do that.

              G 3 2 Replies Last reply
              0
              • A amitmohanty

                I really wanted to use something which can allow me to interchange position of two rows in a 2D array. say i want to make 3rd row 7th row and place 7th row as the third row. I don't want to use a temporary 1Darray for that. If each row has say 1000 elements, then i need 1000 temporary varibale to do the calculation. I don't want to do that.

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                What does the array contain? If it contains objects, it's really an array of references. Then you can easily exchange different elements in the array. --- b { font-weight: normal; }

                A 1 Reply Last reply
                0
                • G Guffa

                  What does the array contain? If it contains objects, it's really an array of references. Then you can easily exchange different elements in the array. --- b { font-weight: normal; }

                  A Offline
                  A Offline
                  amitmohanty
                  wrote on last edited by
                  #8

                  Ironically, it contains double precision numerical value. Is there a way? How can I change the reference if it consists of objects. Regards, Amit

                  G 1 Reply Last reply
                  0
                  • A amitmohanty

                    I really wanted to use something which can allow me to interchange position of two rows in a 2D array. say i want to make 3rd row 7th row and place 7th row as the third row. I don't want to use a temporary 1Darray for that. If each row has say 1000 elements, then i need 1000 temporary varibale to do the calculation. I don't want to do that.

                    3 Offline
                    3 Offline
                    3Dizard
                    wrote on last edited by
                    #9

                    Well, arrays in C# can be seen as pointers. If you copy one via assigning another one (e.g. a1 = a2) then you don't get a real copy a2, you just get a new reference. Greetings

                    1 Reply Last reply
                    0
                    • A amitmohanty

                      Ironically, it contains double precision numerical value. Is there a way? How can I change the reference if it consists of objects. Regards, Amit

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #10

                      A double is a value type, not an object, so the array does not contain references. You have to exchange the values. --- b { font-weight: normal; }

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups