Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting from one pointer to pointer type to another in Golang error

Tags:

Can anyone tell my why this wouldn't compile?

package main

type myint int
func set(a **myint) {
    i := myint(5)
    *a = &i 
}

func main() {
    var k *int
    set( (**myint)(&k) ) // cannot convert &k (type **int) to type **myint
    print( *k )
}

My reasoning so far is this. All types in Golang are different, but it allows to convert from one type to another with C-like cast syntax as long as underlying types are identical. In my example, converting 'int' to 'myint' is not a problem. '*int' to '*myint' isn't either. It's when you have pointer to pointer problems arise. I've been stuck on this for the second day now. Any help is appreciated.

like image 238
Gunchars Avatar asked Mar 20 '11 10:03

Gunchars


People also ask

What happens when you typecast a pointer?

An open (void) pointer can hold a pointer of any type. Casting an open pointer to other pointer types and casting other pointer types to an open pointer does not result in a compile time error. Note: You might receive a runtime exception if the pointer contains a value unsuitable for the context.

Can you define a pointer to pointer in Go?

A pointer to a pointer is a form of chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.

What is the type of a pointer in Go?

In Go a pointer is represented using the * (asterisk) character followed by the type of the stored value. In the zero function xPtr is a pointer to an int . * is also used to “dereference” pointer variables.

Can we type cast pointers?

We saw that pointer values may be assigned to pointers of same type. However, pointers may be type cast from one type to another type. In the following code lines, A is an int type variable, D is variable of type double, and ch is a variable of type char.


1 Answers

Here's my analysis.

(**myint)(&k) -- cannot convert &k (type **int) to type **myint:

type **int and type **myint are unnamed pointer types and their pointer base types, type *int and type *myint, don't have identical underlying types.

If T (*int or *myint) is a pointer type literal, the corresponding underlying type is T itself.

(*myint)(k) -- can convert k (type *int) to type *myint:

type *int and type *myint are unnamed pointer types and their pointer base types, type int and type myint (type myint int), have identical underlying types.

If T (int) is a predeclared type, the corresponding underlying type is T itself. If T (myint) is neither a predeclared type or nor a type literal, T's underlying type is the underlying type of the type to which T refers in its type declaration (type myint int).

(myint)(*k) -- can convert *k (type int) to type myint:

type int and type myint have identical underlying types.

If T (int) is a predeclared type, the corresponding underlying type is T itself. If T (myint) is neither a predeclared type or nor a type literal, T's underlying type is the underlying type of the type to which T refers in its type declaration (type myint int).

Here's the underlying type example from the Types section revised to use integers and int pointers.

type T1 int
type T2 T1
type T3 *T1
type T4 T3

The underlying type of int, T1, and T2 is int. The underlying type of *T1, T3, and T4 is *T1.

References:

The Go Programming Language Specification

Conversions

Types

Properties of types and values

Type declarations

Predeclared identifiers

Pointer Type

like image 176
peterSO Avatar answered Oct 15 '22 02:10

peterSO