使用指向数组的指针 [英] Using a pointer to array

查看:91
本文介绍了使用指向数组的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Google的Go语言有一点小小的兴趣,并且我遇到了一些在C中相当基本的东西,但似乎并没有包含在我目前看到的文档中。



当我将一个指针传递给一个函数的切片时,我推测我们可以通过以下方式访问它:



<$对于i:= 0,p $ p $ func conv(x [] int,xlen int,h [] int,hlen int,y * [] int)

; I< XLEN; i ++ {
for j:= 0; J<为hLen; j ++ {
* y [i + j] + = x [i] * h [j]
}
}
}



但Go编译器不喜欢这样:

  sean @ spray:〜/ dev $ 8g broke.go 
broke.go:8:无效操作:y [i + j](类型索引* [] int)

公平 - 这只是一个猜测。我有一个相当直接的解决方法:

pre $ func conv(x [] int,xlen int,h [] int,hlen int ,y_ * [] int){
y:= * y_

for i:= 0; I< XLEN; i ++ {
for j:= 0; J<为hLen; j ++ {
y [i + j] + = x [i] * h [j]
}
}
}
pre>

但是当然有更好的方法。令人讨厌的是,Go上的Googling信息并不是非常有用,因为大多数搜索术语都会出现各种C / C ++ /不相关的结果。

解决方案

Google Go文档声明传递数组 - 他们说你通常想传递一个片段(而不是指针?):

更新:



正如@ Chickencha的评论所指出的那样,数组切片是引用,这就是为什么它们传递效率高的原因。因此,您可能希望使用分片机制而不是原始指针。 来自 Google有效Go文档 rel = nofollow noreferrer> http://golang.org/doc/effective_go.html#slices


切片是引用类型,






原始



它在标题下

关于类型的插曲


[... snip ...]当将一个数组
传递给函数时,您几乎总是希望
将形式参数声明为
a片。当你调用这个函数时,
获取数组的地址,Go
将创建(高效)一个slice
引用并传递它。


编者按:不再是这种情况



使用切片可以编写这个函数( from sum.go):

  09 func sum(a [] int)int {//返回一个int 
10 s:= 0
11 for i:= 0;我< LEN(一); i ++ {
12 s + = a [i]
13}
14回报s
15}

并像这样调用它:

  19 s:= sum(& [3] int {1,2,3})//数组的一个片段被传递给总和



< hr>

也许可以将整个数组作为切片。 Google表示Go与切片有效交易。这是这个问题的另一个答案,但也许这是一个更好的方法。


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 a slice 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 := 0; i<xlen; i++ {
        for j := 0; 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:8: 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 := 0; i<xlen; i++ {
        for j := 0; 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 Google Go docs state the following about passing arrays - they say you usually want to pass a slice (instead of a pointer?):

Updated:

As indicated by @Chickencha's comment, array slices are references which is why they are efficient for passing. Therefore likely you will want to use the slice mechanism instead of "raw" pointers.

From Google Effective Go doc http://golang.org/doc/effective_go.html#slices

Slices are reference types,


Original

It's under the heading

An Interlude about Types

[...snip...] When passing an array to a function, you almost always want to declare the formal parameter to be a slice. When you call the function, take the address of the array and Go will create (efficiently) a slice reference and pass that.

Editor's note: This is no longer the case

Using slices one can write this function (from sum.go):

09    func sum(a []int) int {   // returns an int
10        s := 0
11        for i := 0; i < len(a); i++ {
12            s += a[i]
13        }
14        return s
15    }

and invoke it like this:

19        s := sum(&[3]int{1,2,3})  // a slice of the array is passed to sum    


Maybe pass the whole array as a slice instead. Google indicates Go deals efficiently with slices. This is an alternate answer to the question but maybe it's a better way.

这篇关于使用指向数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆