Go每日一题(58) 的题目如下

3975 次点击 · 5 赞 ·大约8小时之前 开始浏览   · 来源「Golang来啦」

下面代码输出什么?

func incr(p *int) int {
	*p++
	return *p
}

func main() {
	p :=1
	incr(&p)
	fmt.Println(p)
}
  • A. 1
  • B. 2
  • C. 3
3975 阅读
46 回复
Voryla
Voryla · #1 · 3年之前

get

euibieur894
euibieur894 · #2 · 3年之前

打卡

Natsuwau
Natsuwau · #3 · 3年之前

mark

Howe
Howe · #4 · 3年之前

Get

Neightly
Neightly · #5 · 3年之前

其实也并不是那么显然的事。

Unary operators have the highest precedence. As the ++ and -- operators form statements, not expressions, they fall outside the operator hierarchy. As a consequence, statement *p++ is the same as (*p)++.

如果没有上述保底,也许就是*(p++)了。

henry1
henry1 · #6 · 3年之前

打卡

jan-bar
jan-bar · #7 · 3年之前
NeightlyNeightly #5 回复

其实也并不是那么显然的事。 `Unary operators have the highest precedence. As the ++ and -- operators form statements, not expressions, they fall outside the operator hierarchy. As a consequence, statement *p++ is the same as (*p)++.` 如果没有上述保底,也许就是`*(p++)`了。

go不支持直接对指针进行运算,所以不用考虑你这种情况,这大概就是大道至简吧。

brothersam
brothersam · #8 · 3年之前

谁要这么写代码会被老板打死。返回int,却return一个指针;函数有返回值调用的时候却没有承接的变量。(golang码农求职)

zzustu
zzustu · #9 · 3年之前
brothersambrothersam #8 回复

谁要这么写代码会被老板打死。返回int,却return一个指针;函数有返回值调用的时候却没有承接的变量。(golang码农求职)

有没有一种可能:作者的用意涵盖了开发中常用的 struct*struct,只是为了突出问题重点,用了 int*int 作为演示

zzustu
zzustu · #10 · 3年之前
NeightlyNeightly #5 回复

其实也并不是那么显然的事。 `Unary operators have the highest precedence. As the ++ and -- operators form statements, not expressions, they fall outside the operator hierarchy. As a consequence, statement *p++ is the same as (*p)++.` 如果没有上述保底,也许就是`*(p++)`了。

题目代码可以运行并输出(go1.18),就算需要加括号,应该也是 (*p)++

lwcbest
lwcbest · #11 · 3年之前

这题不能想复杂了

ByteDance
ByteDance · #12 · 3年之前

打卡

neil_liu
neil_liu · #13 · 3年之前

打卡

hasbug
hasbug · #14 · 3年之前

mark

a406299736
a406299736 · #15 · 3年之前

打卡。。。。。。

AntonyZhang
AntonyZhang · #16 · 3年之前

mark

jatshw
jatshw · #17 · 3年之前

打卡

wzbwzt
wzbwzt · #18 · 3年之前

1

mingtop
mingtop · #19 · 3年之前

*(p++) 这种写法编译不通过的

brothersam
brothersam · #20 · 3年之前
zzustuzzustu #9 回复

#8楼 @brothersam 有没有一种可能:作者的用意涵盖了开发中常用的 `struct` 和 `*struct`,只是为了突出问题重点,用了 `int` 和 `*int` 作为演示

关键是,程序能编译通过,还能运行起来

zzustu
zzustu · #21 · 3年之前
brothersambrothersam #20 回复

#9楼 @zzustu 关键是,程序能编译通过,还能运行起来

仔细看:

func incr(p *int) int {
    *p++
    return *p
}

*int代表类型。*p代表解引用。

brothersam
brothersam · #22 · 3年之前
zzustuzzustu #21 回复

#20楼 @brothersam 仔细看: ```go func incr(p *int) int { *p++ return *p } ``` `*int`代表类型。`*p`代表解引用。

incr(&p) 我说的是这里,函数回来的值,没有变量承接

NovaChaos
NovaChaos · #23 · 2年之前

mk

hades2013
hades2013 · #24 · 2年之前

打卡学习

brothersam
brothersam · #25 · 2年之前

2

feiyang
feiyang · #26 · 2年之前

*p++

hasbug
hasbug · #27 · 2年之前

mark

a406299736
a406299736 · #28 · 2年之前

mk

huangyf168
huangyf168 · #29 · 2年之前

mark

wzbwzt
wzbwzt · #30 · 2年之前

1

Zuos
Zuos · #31 · 2年之前

mark

xutao
xutao · #32 · 2年之前

mark

huangyf168
huangyf168 · #33 · 2年之前

mark

YuPeng
YuPeng · #34 · 2年之前

打卡

hasbug
hasbug · #35 · 2年之前

mark

bsdx866
bsdx866 · #36 · 2年之前

image.png

feiyang
feiyang · #37 · 2年之前

Dereference operator 解引用

YuPeng
YuPeng · #38 · 2年之前

mark

hasbug
hasbug · #39 · 2年之前

mark

huangyf168
huangyf168 · #40 · 2年之前

mark

Pwee
Pwee · #41 · 2年之前
brothersambrothersam #8 回复

谁要这么写代码会被老板打死。返回int,却return一个指针;函数有返回值调用的时候却没有承接的变量。(golang码农求职)

加上*取值符号不就是return了一个int值,并不是指针啊

jiftle
jiftle · #42 · 2年之前

2

TimLiuDream
TimLiuDream · #43 · 2年之前

mark

zhangwei_go
zhangwei_go · #44 · 大约1年之前

func incr(p int) int { p++ return *p }

func main() { p :=1 incr(&p) fmt.Println(p) }

// 结果: 2

BigBigGopher
BigBigGopher · #45 · 大约1年之前

mark

Qmfuser
Qmfuser · #46 · 大约1年之前

mark

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传