how to define a pointer to an array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Demeyer

    how to define a pointer to an array?

    I can't manage to define a simple pointer to an array.
    The following C++ code doesn't work in C# (compiler error: "You can
    only take the address of unfixed expression inside of a fixed statement
    initializer")

    Int32 *pointer;
    int []array = new int[100];
    pointer = &array[0];

    Can anyone please instruct me how to do this properly in C#?
    Thanks,
    Peter

  • Mattias Sjögren

    #2
    Re: how to define a pointer to an array?

    Peter,
    [color=blue]
    >Can anyone please instruct me how to do this properly in C#?[/color]

    If you really have to do it:

    unsafe {
    fixed (int* pointer = array) {
    // Do stuff here
    }
    }


    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Brendan Grant

      #3
      RE: how to define a pointer to an array?

      You can define a pointer to an array... but only within a limited area by
      using the fixed statement
      (http://msdn.microsoft.com/library/de...vclrffixed.asp) ... in this case you would do it like this:

      int []array = new int[100];
      fixed(int* pointer = &array[0])
      {
      //use the pointer
      }

      By using the fixed keyword, you are telling the CLR that you want to force
      it not to move the data that the pointer is pointing at around in memory
      (which is a real risk with managed code).

      Be sure to note that the pointer is only usable within with in the statement
      (ie between { and }) immediately following the fixed keyword, so you cannot
      take this pointer with you... however it is still usable for things like
      P/Invokes and some pointer arithmetic.

      Brendan


      "Peter Demeyer" wrote:
      [color=blue]
      > I can't manage to define a simple pointer to an array.
      > The following C++ code doesn't work in C# (compiler error: "You can
      > only take the address of unfixed expression inside of a fixed statement
      > initializer")
      >
      > Int32 *pointer;
      > int []array = new int[100];
      > pointer = &array[0];
      >
      > Can anyone please instruct me how to do this properly in C#?
      > Thanks,
      > Peter
      >
      >[/color]

      Comment

      • Ron

        #4
        RE: how to define a pointer to an array?

        Here's how you do it:


        "Peter Demeyer" wrote:
        [color=blue]
        > I can't manage to define a simple pointer to an array.
        > The following C++ code doesn't work in C# (compiler error: "You can
        > only take the address of unfixed expression inside of a fixed statement
        > initializer")
        >
        > Int32 *pointer;
        > int []array = new int[100];
        > pointer = &array[0];
        >
        > Can anyone please instruct me how to do this properly in C#?
        > Thanks,
        > Peter
        >
        >[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: how to define a pointer to an array?

          Peter Demeyer <demeyer.peter@ gmail.com> wrote:[color=blue]
          > I can't manage to define a simple pointer to an array.
          > The following C++ code doesn't work in C# (compiler error: "You can
          > only take the address of unfixed expression inside of a fixed statement
          > initializer")
          >
          > Int32 *pointer;
          > int []array = new int[100];
          > pointer = &array[0];
          >
          > Can anyone please instruct me how to do this properly in C#?[/color]

          Others have given you the answer of how - could I ask *why* you need to
          do this? It could well be that there's an answer to the problem you're
          trying to tackle that doesn't require pointers.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Peter Demeyer

            #6
            Re: how to define a pointer to an array?

            Thank you all very much!
            I managed to do what I wanted with your help.
            Peter

            Comment

            Working...