Go: using a pointer to array
2024-09-05 13:57:26
下面的不是指针指向数组,而是指针指向Slice
I'm having a little play with google's Go language, and I've run into something which is fairly basic in C but doesn't seem to be covered in the documentation I've seen so far.
When I pass a pointer to an array to a function, I presumed we'd have some way to access it as follows:
func conv(x []int, xlen int, h []int, hlen int, y *[]int) for i := ; i<xlen; i++ {
for j := ; j<hlen; j++ {
*y[i+j] += x[i]*h[j]
}
}
}
But the Go compiler doesn't like this:
sean@spray:~/dev$ 8g broke.go
broke.go:: invalid operation: y[i + j] (index of type *[]int)
Fair enough - it was just a guess. I have got a fairly straightforward workaround:
func conv(x []int, xlen int, h []int, hlen int, y_ *[]int) {
y := *y_ for i := ; i<xlen; i++ {
for j := ; j<hlen; j++ {
y[i+j] += x[i]*h[j]
}
}
}
But surely there's a better way. The annoying thing is that googling for info on Go isn't very useful as all sorts of C\C++\unrelated results appear for most search terms.
下面是解决方案:
the semicolon and the asterisk are added and removed.
*y[i+j] += x[i]*h[j]
-->
(*y)[i+j] += x[i] * h[j];
最新文章
- 手机开发中的AP与BP的概念
- iPhone添加邮箱
- js基础知识:变量
- mysql多实例(个人的情况,不是大众的)里面有配置好的脚本+主从复制
- 电梯调度--c++--软件工程
- WordPress Platinum SEO插件跨站脚本漏洞
- The account is locked
- poj 1872 A Dicey Problem WA的代码,望各位指教!!!
- Vmware虚拟机安装Ubuntu 16.04 LTS(长期支持)版本+VMware tools安装
- jquery按钮倒计时
- 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure(对于二分类问题)
- 《android开发艺术探索》读书笔记(十四)--JNI和NDK编程
- Python基础之函数和模块
- vue样式控制的方式
- idea代码回退到前面的版本
- nginx 目录讲解
- nginx 504 Gateway Time-out
- VS2013新建web项目时出错,系统找不到指定文件
- Luogu4783 【模板】矩阵求逆(高斯消元)
- POJ 1830 开关问题(高斯消元)题解