I have a string and i want to copy at specified memory location.
Suppose i have a pre-allocated memory represented by

char* logFile (unsafe code)

I have a generated string which contains the name of log file.
I want to copy that string on location represented by char*.

Please help me on this.
========================================================================

Also i have to copy specified number of characters from a given location
to specified location.

The code in delphi looks something like:

StrLCopy(msg, PChar(gsRootErrorMsg), MAX_ERR_MESSAGE);
where msg is character pointer:msg: PChar
and gsRootErrorMsg is string.


Help would be really appreciated.
Thanks.

Recommended Answers

All 4 Replies

Try this,

static void Main(string[] args)
        {
            unsafe
            {
                string str = "Hello World";
                char* pstr=stackalloc char[str.Length + 1];

                int i = 0;
                for(i=0;i<str.Length;i++)
                {
                    *(pstr + i) = str[i];
                }

                *(pstr+i) = '\u0000';

                
                while (*pstr != '\u0000')
                    Console.WriteLine(*pstr++);
                
            }
        }

Perfect ..
It worked ..
Thanks

Now second thing is that i need to copy the contents from a specified memory location (source) to specified memory location (destination) upto specified Max number of characters.

Help would be really appreciated.

Not so difficult,

*(pstr+2)=str[0];
*(pstr+1)=str[2];
*(pstr+3)=str[4];
*(pstr+0)=str[1];
*(pstr+4)='\u0000';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.