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

查看:122
本文介绍了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

在*指针和与放大器之间指针的符号,我很迷茫。我知道*指针指给任何指向。

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

难道我把无效(INT *指针)在声明中。约当我使用的功能是什么?

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

您的援助AP preciated。

Your assistance is appreciated.

编辑2:

好吧,我现在明白了,使用*在声明变量意味着一个指针将被传递。然而,对于当我使用的功能是什么?

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:

根据以上code的约定:

According to the conventions of the above code:

栏=安培; foo的意思是:让酒吧点在内存中为foo

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

*巴= 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:

酒吧= OFF表示:酒吧指向OOF指针

bar = oof means: bar points to the oof pointer

栏= *钱币装置:杆指向的值OOF点,但不适用于OOF指针本身

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

*巴= * OOF是指:变更值栏点的值,OOF指向

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

&放大器;巴=安培;钱币装置:改变存储器地址栏点是相同的存储器地址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的答案,但我有第一个去,我不知道一个社区维基是怎么工作的,但我会离开它。这种方式对于编辑(随意把它变成一个参考指南,如果你喜欢)。

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;

方式顺从的指针,并使其在等于三指向的结构,而不是使指针等于存储器地址0x的0003

Means deference 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;
}

在函数声明,*表示要传递一个指针,但它的实际code身体*意味着你正在访问哪些指针所指向。

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;

意味着杆指针等于一个整数,而不是什么酒吧点,这是无效的。

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

&安培;巴=安培; OOF;

&是像一个功能,一旦它返回您不能修改它是从哪里来的内存地址。就这样code:

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

不等于栏指向OOF指针这意味着,酒吧指向的地址OOF点,所以酒吧点,无论是富指着 - 不会吧点到foo指向OOF。

Does not mean that "bar points to the oof pointer" 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天全站免登陆