Int point to string

Posted on 2006-04-20
Last Modified: 2010-04-16
Is there a way in C# to create an int pointer to a string or char array, for example:

string whatever = "test";
int * ptr = &whatever[0];

I need to then pass this address to a function which accepts int as input.  Thanks ahead of time.  Nick

Question by:nstefanov
    LVL 48

    Expert Comment

    What function? Is this API or managed function? Please give more details.

    Author Comment

    Just a custom function, something like this:

    int customfunction(int firstaddress, int lastaddress)
       if (firstaddress == lastaddress)
           do something....
          do something else
       return some int;

    Then I want to pass from above &whatever[0], &whatever[whatever.lenght] like so

    int result = customfunction(&whatever[0], &whatever[last]);


    LVL 48

    Accepted Solution

    I think that you are trying to reproduce some C++ algorithms in C#. But .NET string cannot be changed once it is creted, every function which is applied to string creates new string instance. I don't see a way to apply unsafe pointer to string characters, because string [] operator gives string character by value, and address of it is not address of string.
    Take a look at String.ToCharArray function and String (Char[])  constructor, maybe this algorithm can work on char array.
    LVL 37

    Assisted Solution

    You can use string builder to marshal as an edittable string I believe.


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now