Pointers and Functions

agolangf · 2018-05-27 12:30:11 · 1980 次点击    
这是一个分享于 2018-05-27 12:30:11 的资源,其中的信息可能已经有所发展或是发生改变。

I have 2 questions, may be somewhat noob.

  1. When I need to pass pointer reference to a function(as arguments) and why?
  2. When I need to return pointer reference from a function and why?

Thanks a lot


评论:

Aeaex:

Do you know what a pointer actually is? Might be better to start there. Have you done the Tour?

CrappyFap69:

Of course I know what pointer is. But I ask the question from ground up to understand everything better

0xjnml:

I think the question stems from the confusion some people have with pointer vs. reference. But the term 'pointer reference' beats that by a long way IMO.

CapableCounteroffer:

When I need to pass pointer reference to a function(as arguments) and why?

An example of why you would want to do this is if you want the function to change what the pointer references. For example, say you have a pointer to an int called i, and it points to the address of something holding the value 5. Then in your function you take its single int argument and add 5 to it. You call the function on i as a value, then you check i. What do you get? i==5. You call the function on i passed as a pointer reference, then you check i. What do you get? i==10 as probably intended.

CrappyFap69:

That's great. Any other reason? And what about the second question?


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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