dongzhuo3376 2016-08-22 07:54
浏览 70

在Golang中附加指向切片的指针

I want to append a pointer to a slice.Is it possible..?In Partentnode.children is a slice I want to append it with X as pointer.

https://play.golang.org/p/ghWtxWGOAU

func Tree(Parentnode *Node) {
    if IsvisitedNode(Parentnode.currentvalue - 1) {
        m := MovesArray[Parentnode.currentvalue-1]
        for j := 0; j < 8; j++ {
            if m[j] != 0 {
                var X *Node
                X.parentnode = Parentnode
                X.currentvalue = m[j]
                if IsvisitedNode(m[j]) {
                    Parentnode.children = append(Parentnode.children, *X)
                    Tree(X)
                }
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dtz55359 2016-08-22 09:50
    关注

    You have a off by one error. In main you set Y.currentvalue = 1. Then in Tree currentvalue walks to 64.

    X.currentvalue = m[j]
    fmt.Printf("cv: %v
    ",X.currentvalue) //walks to 64
    if IsvisitedNode(m[j]) {
    

    An in IsvisitedNode you test that index against visithistory that has 64 indexes, thus stops at index 63. -> index error

    var visithistory [64]bool
    
    func IsvisitedNode(position int) bool {
        if visithistory[position] == true {
    

    Things work if you set var visithistory [65]bool but I think you need to rethink you logic here somewhat.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法