C++ 将指针传递给函数 (Howto) + C++ 指针操作 [英] C++ Passing Pointer to Function (Howto) + C++ Pointer Manipulation

查看:51
本文介绍了C++ 将指针传递给函数 (Howto) + C++ 指针操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对传递指针的工作方式有点困惑.

I am a little confused as to how passing pointers works.

假设我有以下函数和指针,并且...

Let's say I have the following function and pointer, and...

编辑:

...我想使用指向某个对象的指针作为函数中的参数.

...I want to use a pointer to some object as an argument in the function.

即:

void Fun(int Pointer){
    int Fun_Ptr = ---Passed Pointer---; 
    //So that Fun_Ptr points to whatever ---Passed Pointer points to

在 *Pointer 和 &Pointer 符号之间,我很困惑.我知道 *Pointer 意味着给出它指向的任何东西.

Between the *Pointer and &Pointer notations, I am very confused. I know that *Pointer means give whatever it points to.

我是否在声明中放置了 void (int *pointer).当我使用该功能时呢?

Do I put void (int *pointer) in the declaration. What about when I use the function?

感谢您的帮助.

编辑 2:

好的,我现在明白在声明中使用 *variable 意味着将传递一个指针.但是,当我使用该功能时呢?

Okay, I now understand that using *variable in a declaration means that a pointer will be passed. However, what about when i use the function?

int main(){
    int foo;
    int *bar;
    bar = foo;
    Fun(bar);
}

编辑 3:好的,如果我错了,请纠正我:

EDIT 3: Okay, so correct me if I am wrong:

根据上面代码的约定:

bar = &foo 表示:让 bar 指向内存中的 foo

bar = &foo means: Make bar point to foo in memory

*bar = foo 表示将 bar 指向的值更改为等于 foo 等于的任何值

*bar = foo means Change the value that bar points to to equal whatever foo equals

如果我有第二个指针 (int *oof),那么:

If I have a second pointer (int *oof), then:

bar = oof 表示:bar 指向 oof 指针

bar = oof means: bar points to the oof pointer

bar = *oof 表示:bar 指向 oof 指向的值,但不指向 oof 指针本身

bar = *oof means: bar points to the value that oof points to, but not to the oof pointer itself

*bar = *oof 表示:将bar指向的值改为oof指向的值

*bar = *oof means: change the value that bar points to to the value that oof points to

&bar = &oof 意思是:将bar指向的内存地址改为与oof指向的内存地址相同

&bar = &oof means: change the memory address that bar points to be the same as the memory address that oof points to

我有这个权利吗?

编辑 4:非常感谢您的所有帮助(我希望我能接受 1 个以上的答案,但我必须选择第一个.我不确定社区 wiki 是如何工作的,但我会留下它以这种方式进行编辑(如果您愿意,可以随意将其转换为参考指南).

EDIT 4: Thanks so much for all your help (I wish I could accept more than 1 answer, but I have to go with the first one. I am not sure how a community wiki works exactly, but I will leave it this way for editing (feel free to turn it into a ref guide if you like).

推荐答案

定义变量和使用变量时 * 的用法是不同的.

There is a difference in the * usage when you are defining a variable and when you are using it.

在声明中,

int *myVariable;

表示指向整数数据类型的指针.然而,在使用中,

Means a pointer to an integer data type. In usage however,

*myVariable = 3;

表示解引用指针,使其指向的结构等于3,而不是使指针等于内存地址0x 0003.

Means dereference the pointer and make the structure it is pointing at equal to three, rather then make the pointer equal to the memory address 0x 0003.

所以在你的函数中,你想这样做:

So in your function, you want to do this:

void makePointerEqualSomething(int* pInteger)
{
    *pInteger = 7;
}

在函数声明中,* 表示您正在传递一个指针,但在其实际代码体中,* 表示您正在访问指针所指向的内容.

In the function declaration, * means you are passing a pointer, but in its actual code body * means you are accessing what the pointer is pointing at.

为了消除您的任何困惑,我将简要介绍与符号 (&)

In an attempt to wave away any confusion you have, I'll briefly go into the ampersand (&)

&意味着获取某物的地址,它在计算机内存中的确切位置,所以

& means get the address of something, its exact location in the computers memory, so

 int & myVariable;

在声明中表示一个整数的地址,或者一个指针!

In a declaration means the address of an integer, or a pointer!

不过这个

int someData;    
pInteger = &someData;

意味着使 pInteger 指针本身(记住,指针只是它们指向的内存地址)等于 'someData' 的地址 - 所以现在 pInteger 将指向一些数据,并且可以在您访问它时使用它尊重它:

Means make the pInteger pointer itself (remember, pointers are just memory addresses of what they point at) equal to the address of 'someData' - so now pInteger will point at some data, and can be used to access it when you deference it:

*pInteger += 9000;

这对你有意义吗?还有什么让您感到困惑的吗?

Does this make sense to you? Is there anything else that you find confusing?

@Edit3:

几乎正确,除了三个陈述

Nearly correct, except for three statements

bar = *oof;

表示bar指针等于一个整数,而不是bar指向的,无效.

means that the bar pointer is equal to an integer, not what bar points at, which is invalid.

&bar = &oof;

&符号就像一个函数,一旦它返回一个内存地址,你就不能修改它的来源.就像这段代码:

The ampersand is like a function, once it returns a memory address you cannot modify where it came from. Just like this code:

returnThisInt("72") = 86; 

无效,你的也是无效的.

Is invalid, so is yours.

最后,

bar = oof

并不意味着bar 指向 oof 指针".相反,这意味着 bar 指向 oof 指向的地址,所以 bar 指向 foo 指向的任何东西 - 而不是 bar 指向指向 oof 的 foo.

Does not mean that "bar points to the oof pointer." Rather, this means that bar points to the address that oof points to, so bar points to whatever foo is pointing at - not bar points to foo which points to oof.

这篇关于C++ 将指针传递给函数 (Howto) + C++ 指针操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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