只能获取 fixed 语句初始值设定项内的未固定表达式的地址

yanglei492064776 2009-10-04 08:58:38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// compile with: /unsafe;

namespace myPerm
{
class Program
{
// delegate void ProcessDelegate(int x, int y);

public unsafe static void swap(int *i, int *j)
{

int temp = *i;
*i = *j;
*j = temp;
}

static int n=0 ;
public unsafe static int Perm(int[] a, int k, int m)
{
// ProcessDelegate myDelegate = new ProcessDelegate(swap);
if (k > m)
{

for (int i = 0; i <= m; i++)
{
Console.Write(" " + a[i] + " ");
}
Console.WriteLine();
n++;
}
else
{
for (int i = k; i <= m; i++)
{
swap(&a[k], &a[i]);
Perm(a, k + 1, m);
swap(&a[k], &a[i]);
}
}
return n;
}
static void Main(string[] args)
{
int[] list = new int[] { 1, 2, 3, 4, 5 };

int n = Perm(list, 0, 4);

Console.WriteLine("The total are {0} times", n);
Console.ReadKey();
}
}
}
...全文
2011 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
随风秀舞 2012-08-06
  • 打赏
  • 举报
回复
如果我想把整个char[] name 数组变换为char*的话,
用fixed(char * ptr = name)却是不行。为什么呀?
骑行大地 2011-06-03
  • 打赏
  • 举报
回复
同样学习。fixed语句主要用来防止系统进行“总动内存管理”时移动或释放被某个指针变量所指向的变量。
骑行大地 2011-06-03
  • 打赏
  • 举报
回复
修改如下:

public unsafe static int Perm(int[] a, int k, int m)
{
// ProcessDelegate myDelegate = new ProcessDelegate(swap);
if (k > m)
{

for (int i = 0; i <= m; i++)
{
Console.Write(" " + a[i] + " ");
}
Console.WriteLine();
n++;
}
else
{
for (int i = k; i <= m; i++)
{
fixed (int* p1 = &a[k], p2 = &a[i])
{
//swap(&a[k], &a[i]);
swap(p1, p2);
}
Perm(a, k + 1, m);
fixed (int* p1 = &a[k], p2 = &a[i])
{
//swap(&a[k], &a[i]);
swap(p1, p2);
}
}
}
return n;
}

其他不变。

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧