Q:

Golang Pointers | Find Output Programs | Set 2

0

This section contains the Golang pointers find output programs (set 2) with their output and explanations.

Program 1:

package main

import "fmt"

func main() {
	var val string = "hello"
	var ptr *string

	ptr = &val
	fmt.Printf("%s\n", *ptr)
	fmt.Printf("%c\n", ptr[1])
}

Program 2:

package main

import "fmt"

func main() {
	var s *int
	fmt.Println("s = ", s)
}

Program 3:

package main

import "fmt"

func main() {
	x := 10
	var s *int

	s = &x

	if s == &x {
		fmt.Println("True...")
	} else {
		fmt.Println("False...")
	}
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

./prog.go:11:24: invalid operation: ptr[1] (type *string does not support indexing)

Explanation:

The above program will generate a syntax error because we cannot indexing with string pointer.


 

Answer Program 2:

Output:

s =  <nil>

Explanation:

The variable s is uninitialized, and the uninitialized pointer variable's value is nil.


 

Answer Program 3:

Output:

True...

Explanation:

 

Here, the variable s is an integer pointer and x is an integer variable, s is initialized with the address of x, s contains the address of x. Thus, the condition is true.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now