如何将指针从函数传递给另一个? [英] How to pass a pointer from a function to an other??

查看:393
本文介绍了如何将指针从函数传递给另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在使用函数A中的指针并且需要它们可以访问函数B


函数B似乎没有相同的值

plz help

am working with pointers in function A and need them to be accessible for function B

the function B seems to not have the same values
plz help

推荐答案

我不确定你的情况。你是否从A到B传递指针? B到A? A和B?每种情况都有自己需要注意的危险。


更好的描述和代码片段会有所帮助。
I''m not sure of your situation. Are you passing pointers from A to B? B to A? A and B? Each situation has it own hazards to watch for.

A better description and a code snippet would help.


一般情况下,如果需要两个函数共享大量数据,最好的解决方案是使它们成为C ++类。


如果您使用的是C,您还可以创建一个结构来托管一堆参数,从而简化参数传递。

Generally, if two functions need to share a lot of data, the best solution is to make them a C++ class.

If you are using C, you could also create a structure to host a bunch of parameters, therefore simplifying parameter passing.

展开 | 选择 | Wrap | 行号


在函数B中使用函数A中的指针有两种形式:


1)函数B不改变函数A中指针​​的值。

在这种情况下,只需使用函数B中的指针参数传递指针。这将使函数A中指针​​的副本,允许函数B使用该指针内的地址。


2)函数B需要改变函数A中的指针值/>

在这种情况下,使用函数B中的指针到指针参数,将函数A中指针​​的地址传递给函数B.函数B通过取消引用指针到指针的参数将在函数A中进行。
Having the pointers in function A available in function B takes two forms:

1) Function B does not change the value of the pointer in function A.

In this case, just pass the pointer using a pointer argument in function B. This will make a copy of the pointer in function A and that allows function B to use the address inside that pointer.

2) Function B needs to change the value of the pointer in function A

In this case, you pass the address of the pointer in function A to function B using a pointer-to-pointer argument in function B. Any changes made in function B by dereferencing the pointer-to-pointer argument will be made in function A.


这篇关于如何将指针从函数传递给另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆