By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,991 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,991 IT Pros & Developers. It's quick & easy.

unsafe?????????

P: n/a
private void button1_Click(object sender, EventArgs e)
{
unsafe
{
string str = "abcde";
fixed (char* pStr = str)
{
A a = new A(pStr);
a.change();
MessageBox.Show(str.ToString());
}
}
}
}

public class A
{
unsafe char* pStr = (char*)0;
public unsafe A(char* sss)
{
pStr =sss;
}

public unsafe void change()
{
*pStr = "fghijkl";//error,why??????

}
}
Jun 17 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You might try spending a little more thought on your Subject: field for
your posts, so that it better describes the question you have, as well as
on the actual phrasing of your question in the post (for example, putting
more than just the code and "error,why").

Also, the Typographical Conservancy phoned, and they want to know why the
question mark population had a sudden decrease. I'll suggest that you can
help them avoid becoming endangered by not using so many, especially since
putting extra question marks in your post doesn't make the post any more
likely to be answered.

Now, all that said:

On Sun, 17 Jun 2007 09:47:39 -0700, wmhnq <wm***@126.comwrote:
public class A
{
unsafe char* pStr = (char*)0;

public unsafe void change()
{
*pStr = "fghijkl";//error,why??????

}
}
You are assigning a string reference to a character. You didn't post the
compiler error, but I suspect it says exactly that: something about cannot
convert a string to a character.

You might prefer this line of code:

pStr = "fghijkl";

That should work better.

Pete
Jun 17 '07 #2

P: n/a
On Jun 17, 11:47 am, "wmhnq" <w...@126.comwrote:
private void button1_Click(object sender, EventArgs e)
{
unsafe
{
string str = "abcde";
fixed (char* pStr = str)
{
A a = new A(pStr);
a.change();
MessageBox.Show(str.ToString());
}
}
}
}

public class A
{
unsafe char* pStr = (char*)0;
public unsafe A(char* sss)
{
pStr =sss;
}

public unsafe void change()
{
*pStr = "fghijkl";//error,why??????

}
}
I'm not understanding the need for unsafe code in your example.

Jun 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.