doushen1026 2019-02-22 17:35 采纳率: 100%
浏览 49
已采纳

无法通过struct方法替换指针

Got a struct method that returns a pointer:

func (d *DataMap) Get(p Coord) *CellData {
    return &d.Data[p.X+(p.Y*d.Size)]
}

The d.Data is an array of CellData which is a struct with several fields. With this method I can modify the inner value of each field, ie:

example.Get(p).Something = 123

But I cannot do something like this:

example.Get(p) = *yada (yada is a *CellData)

Where I want to replace the pointer with another pointer I get:

cannot assign to example.Get(p)(undefined)

What am I doing wrong? The output of the function is defined, dont' know why I'm getting that error. Right now I fixed this by accessing the array directly.

Thanks.

  • 写回答

1条回答 默认 最新

  • dongta5747 2019-02-22 17:45
    关注

    Get returns a pointer, so if you want to store into whatever that pointer is pointing to, you need to add a * (pointer dereference) at the beginning:

    *(example.Get(p)) = *yada
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline