Using a pointer to array

后端 未结 5 1697
一向
一向 2020-12-05 06:39

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

相关标签:
5条回答
  • 2020-12-05 07:07

    The length is part of the array's type, you can get length of an array by the len() built-in function. So you needn't pass the xlen, hlen arguments.

    In Go, you can almost always use slice when passing array to a function. In this case, you don't need pointers. Actually, you need not pass the y argument. It's the C's way to output array.

    In Go style:

    func conv(x, h []int) []int {
        y := make([]int, len(x)+len(h))
        for i, v := range x { 
            for j, u := range h { 
                y[i+j] = v * u 
            }   
        }   
        return y
    }
    

    Call the function:

    conv(x[0:], h[0:])
    
    0 讨论(0)
  • 2020-12-05 07:15

    Types with empty [], such as []int are actually slices, not arrays. In Go, the size of an array is part of the type, so to actually have an array you would need to have something like [16]int, and the pointer to that would be *[16]int. So, what you are actually doing already is using slices, and the pointer to a slice, *[]int, is unnecessary as slices are already passed by reference.

    Also remember that you can easily pass a slice referring to the entire array with &array (as long as the element type of the slice matches that of the array). (Not anymore.)

    Example:

    package main
    import "fmt"
    
    func sumPointerToArray(a *[8]int) (sum int) {
        for _, value := range *a { sum += value }
        return
    }
    func sumSlice (a []int) (sum int) {
        for _, value := range a { sum += value }
        return
    }
    func main() {
        array := [...]int{ 1, 2, 3, 4, 5, 6, 7, 8 }
        slice := []int{ 1, 2, 3, 4 }
        fmt.Printf("sum arrray via pointer: %d\n", sumPointerToArray(&array))
        fmt.Printf("sum slice: %d\n", sumSlice(slice))
        slice = array[0:]
        fmt.Printf("sum array as slice: %d\n", sumSlice(slice))
    }
    

    Edit: Updated to reflect changes in Go since this was first posted.

    0 讨论(0)
  • 2020-12-05 07:15

    the semicolon and the asterisk are added and removed.

    *y[i+j] += x[i]*h[j]
    -->
    (*y)[i+j] += x[i] * h[j];

    0 讨论(0)
  • 2020-12-05 07:24

    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.

    0 讨论(0)
  • 2020-12-05 07:26

    Here's a working Go program.

    package main
    
    import "fmt"
    
    func conv(x, h []int) []int {
        y := make([]int, len(x)+len(h)-1)
        for i := 0; i < len(x); i++ {
            for j := 0; j < len(h); j++ {
                y[i+j] += x[i] * h[j]
            }
        }
        return y
    }
    
    func main() {
        x := []int{1, 2}
        h := []int{7, 8, 9}
        y := conv(x, h)
        fmt.Println(len(y), y)
    }
    

    To avoid wrong guesses, read the Go documentation: The Go Programming Language.

    0 讨论(0)
提交回复
热议问题