无效操作:类型为*int的索引 golang

huangapple go评论115阅读模式
英文:

Invalid operation: index of type *int golang

问题

**目标:**我一直在使用Go解决《破解编程面试》一书中的第6题。

注意:我不需要这个问题的帮助或解决方案

给定一个由NxN矩阵表示的图像,图像中的每个像素占据4个字节,编写一个方法将图像旋转90度。你能原地完成吗?

**问题:**我创建了一个数组的数组来表示矩阵,并创建了一个交换函数来顺时针交换矩阵中的元素。但是出现了一个非常奇怪的错误,当我尝试编译时:

  1. ./Q6.go:29: invalid operation: b[N - col - 1] (index of type *int)
  2. ./Q6.go:30: invalid operation: b[N - row - 1] (index of type *int)

我从哪里得到了类型为int的索引?在Go文档中,len(v)返回类型为int,而其他所有值都是类型为'int'的,那么我是如何得到类型为int的索引的?

代码:

  1. package main
  2. import "fmt"
  3. func main() {
  4. b := [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} // 4 by 4 array going from 1 to 16
  5. N := len(b)
  6. for row := 0; row < N / 2; row++ {
  7. for col := row; col < N - row - 1; col++ {
  8. a := &b[row][col]
  9. b := &b[col][N - row - 1]
  10. c := &b[N - col - 1][col] // <-- 错误在这里
  11. d := &b[N - row - 1][N - col - 1] // <-- 错误在这里
  12. fourSwap(a, b, c, d)
  13. }
  14. }
  15. for r := range b {
  16. for c:= range b[0] {
  17. fmt.Print(b[r][c])
  18. }
  19. fmt.Print("\n")
  20. }
  21. }
  22. // [a][-][-][b] [c][-][-][a]
  23. // [-][-][-][-] --> [-][-][-][-]
  24. // [-][-][-][-] --> [-][-][-][-]
  25. // [c][-][-][d] [d][-][-][b]
  26. func fourSwap(a, b, c, d *int) {
  27. temp := *b
  28. *b = *a
  29. *a = *c
  30. *c = *d
  31. *d = temp
  32. }
英文:

Objective: I have been solving question 6 from the book 'Cracking the Coding interview' by using Go.

NOTE I DO NOT WAN'T HELP OR SOLUTIONS TO THIS QUESTION

Given an image represented by an NxN matrix, where each pixel in the image is 4
bytes, write a method to rotate the image by 90 degrees. Can you do this in place?

Problem: I made an array of arrays to represent the matrix and I created a swap function to swap the elements clockwise in the matrix. For some reason I get this really weird error when trying to compile:

  1. ./Q6.go:29: invalid operation: b[N - col - 1] (index of type *int)
  2. ./Q6.go:30: invalid operation: b[N - row - 1] (index of type *int)

Where am I getting type *int as an index? In the Go documenation, len(v) returns type int and everything else is in the value of 'N - col - 1' is type int so how am I getting type *int index?

Code:

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. b := [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} // 4 by 4 array going from 1 to 16
  5. N := len(b)
  6. for row := 0; row &lt; N / 2; row++ {
  7. for col := row; col &lt; N - row - 1; col++ {
  8. a := &amp;b[row][col]
  9. b := &amp;b[col][N - row - 1]
  10. c := &amp;b[N - col - 1][col] // &lt;-- Error here
  11. d := &amp;b[N - row - 1][N - col - 1] // &lt;-- Error here
  12. fourSwap(a, b, c, d)
  13. }
  14. }
  15. for r := range b {
  16. for c:= range b[0] {
  17. fmt.Print(b[r][c])
  18. }
  19. fmt.Print(&quot;\n&quot;)
  20. }
  21. }
  22. // [a][-][-][b] [c][-][-][a]
  23. // [-][-][-][-] --&gt; [-][-][-][-]
  24. // [-][-][-][-] --&gt; [-][-][-][-]
  25. // [c][-][-][d] [d][-][-][b]
  26. func fourSwap(a, b, c, d *int) {
  27. temp := *b
  28. *b = *a
  29. *a = *c
  30. *c = *d
  31. *d = temp
  32. }

答案1

得分: 2

你在循环内部声明了b,这就遮蔽了你的切片。

  1. for row := 0; row < N / 2; row++ {
  2. for col := row; col < N - row - 1; col++ {
  3. a := &b[row][col]
  4. b := &b[col][N - row - 1] <<< b现在是一个*int
  5. c := &b[N - col - 1][col] // <-- 这里有错误
  6. d := &b[N - row - 1][N - col - 1] // <-- 这里有错误
  7. fourSwap(a, b, c, d)
  8. }
  9. }
英文:

You declare b inside the loop, and that shadows your slice.

  1. for row := 0; row &lt; N / 2; row++ {
  2. for col := row; col &lt; N - row - 1; col++ {
  3. a := &amp;b[row][col]
  4. b := &amp;b[col][N - row - 1] &lt;&lt;&lt;&lt; b is now an *int
  5. c := &amp;b[N - col - 1][col] // &lt;-- Error here
  6. d := &amp;b[N - row - 1][N - col - 1] // &lt;-- Error here
  7. fourSwap(a, b, c, d)
  8. }
  9. }

答案2

得分: 1

你在出错的那一行之前创建了一个新的本地变量b,它是一个指针:

  1. b := &b[col][N - row - 1]
英文:

You are creating a new local variable b which is a pointer on the line before you get the error:

  1. b := &amp;b[col][N - row - 1]

huangapple
  • 本文由 发表于 2015年5月1日 12:44:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/29981710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定