在函数之间传递指针 [英] Passing Pointer from function to function

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

问题描述

我对理解指针有些陌生,所以我希望有人可以检查一下以帮助我确保我对指针的工作原理有正确的认识.我正在尝试简化什么是指针以及如何使用指针的想法

I'm a bit new to understanding pointers so I was hoping someone could check this to help me make sure I've got the right idea on how pointers work. I'm trying to simplify the idea of what a pointer is and how to use them

functA(int *numb){

functB(numb);

}

functB(int *numb){

functC(numb);

}

functC(int *numb){

*numb+=1;

}

int main (){

int testNumber = 0;

functA(&testNumber);
.
.
.
}

因此,总的来说,我是将testNumber的地址发送给函数A.因此,当我将该地址发送给functA时,没有问题,对吗?

So in main, I'm sending the address of testNumber to funct A. So when I send this address to functA, there is no problem, correct?

由于func A有一个指针参数,可以保存一个地址,所以当我发送地址时它不会出现问题,对吗?然后,由于numb(在functA中)是一个指针,因此我可以将其发送到functB,因为functB可以接受地址.另外,我仍在发送与testNumber相同的地址,对吗?

Since funct A has a pointer parameter, which can hold an address, it won't run into a problem when I send an address, right? Then, since numb (in functA) is a pointer, I can send it to an functB because functB can accept an address. Also, I'm still sending the same address that testNumber is in, correct?

同样,我将numb(从functB发送)到functC,在functC中,我正在取消引用此位置(是否有更好的说法呢?)并将计数增加1.因此,完成此操作后,testNumber应该是1,正确吗?

Again, I send numb (from functB) to functC and in functC, I'm derefencing this location (is there a better way to say this?) and increasing the count by 1. So when this is done, testNumber should be 1, correct?

我知道这听起来可能很愚蠢,但我只是想从概念上理解这一点.我花了比我愿意承认的时间更多的时间来尝试在刚刚提交的项目中弄清这个概念(我做了,但是花了太长时间)

I know this might sound silly and all, but I'm just trying to grasp this conceptually. I spent more time than I'm willing to admit trying to get this down concept on a project I just submitted (I did it, but it took way too long)

推荐答案

变量存储在内存中的某个位置,并且其中存储有一个值.每个位置都有地址.

A variable is stored at a location in memory and has a value stored in it. Every location has address.

variable

location_value

location_address

指针(例如int *p)只是一个像其他变量(例如int a;)一样的变量.区别在于指针存储某个位置的地址(您已经知道).

A pointer (say, int *p) is simply a variable like other variables(say, int a;). Difference is a pointer stores address of some location (which you already know).

variable: int testNumber = 10;

location_value: 10

location_address: 0xABCD000 // <-- 'testNumber' is stored at this location

----------------------------

variable: int *numb = &testNumber;

location_value: 0xABCD000 // <-- note: value of 'numb' is an address, address of 'testNumber'

location_address: 0x1234000 // <-- numb itself is stored at 0x12340000

由于numb是一个变量,您可以继续将其值分配给相同类型的其他变量.

Since, numb is a variable you can go on assigning its value to other variable of same type.

在您的情况下functC::numb = functB::numb = functA::numb = &testNumber = 0xABCD000.这是正确的.

In your case functC::numb = functB::numb = functA::numb = &testNumber = 0xABCD000. which is correct.

因此,当您取消引用*functC::numb时,您将访问存储在地址functC::numb处的变量,该变量恰好是testNumber.因此,当您执行*functC::numb += 1;

So, when you dereference *functC::numb you are accessing the variable which is stored at address functC::numb, which happens to be testNumber. So testNumber gets incremented when you did *functC::numb += 1;

这篇关于在函数之间传递指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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