Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a pointer to array

Tags:

pointers

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.

like image 510
Sean Avatar asked Mar 13 '10 18:03

Sean


People also ask

Can I use a pointer as an array?

So the upshot of that is yes, you can use the [] subscript operator on a pointer expression as well as an array expression. Pointers don't have to be represented as integers - on some older segmented architectures, they were represented as a pair of values (page number and offset).

How do you make an array pointer?

Consider this example: int *ptr; int arr[5]; // store the address of the first // element of arr in ptr ptr = arr; Here, ptr is a pointer variable while arr is an int array. The code ptr = arr; stores the address of the first element of the array in variable ptr .

How can we implement arrays using pointers in C?

Access Array Elements Using Pointers By the way, data[0] is equivalent to *data and &data[0] is equivalent to data. data[1] is equivalent to *(data + 1) and &data[1] is equivalent to data + 1. data[2] is equivalent to *(data + 2) and &data[2] is equivalent to data + 2.


1 Answers

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.

like image 132
John K Avatar answered Oct 08 '22 22:10

John K