C++ 将指针传递给函数(Howto)+ C++ 指针操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3796181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Passing Pointer to Function (Howto) + C++ Pointer Manipulation
提问by Biosci3c
I am a little confused as to how passing pointers works.
我对传递指针的工作方式有些困惑。
Let's say I have the following function and pointer, and...
假设我有以下函数和指针,并且...
EDIT:
编辑:
...I want to use a pointer to some object as an argument in the function.
...我想使用指向某个对象的指针作为函数中的参数。
i.e.:
IE:
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.
在 *Pointer 和 &Pointer 符号之间,我很困惑。我知道 *Pointer 意味着给出它指向的任何东西。
Do I put void (int *pointer) in the declaration. What about when I use the function?
我是否将 void (int *pointer) 放在声明中。当我使用该功能时呢?
Your assistance is appreciated.
感谢您的帮助。
EDIT 2:
编辑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?
好的,我现在明白在声明中使用 *variable 意味着将传递一个指针。但是,当我使用该功能时呢?
i.e.
IE
int main(){
int foo;
int *bar;
bar = foo;
Fun(bar);
}
EDIT 3:Okay, so correct me if I am wrong:
编辑 3:好的,如果我错了,请纠正我:
According to the conventions of the above code:
根据上面代码的约定:
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
*bar = foo 表示将 bar 指向的值更改为等于 foo 等于的任何值
If I have a second pointer (int *oof), then:
如果我有第二个指针 (int *oof),则:
bar = oof means: bar points to the oof pointer
bar = oof 表示:bar 指向 oof 指针
bar = *oof means: bar points to the value that oof points to, but not to the oof pointer itself
bar = *oof 表示:bar 指向的是 oof 指向的值,而不是指向 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
&bar = &oof 意思是:把bar指向的内存地址改成和oof指向的内存地址一样
Do I have this right?
我有这个权利吗?
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).
编辑 4:非常感谢您的所有帮助(我希望我能接受 1 个以上的答案,但我必须采用第一个。我不确定社区 wiki 的工作原理,但我会以这种方式保留编辑(如果您愿意,可以随意将其转换为参考指南)。
回答by Tomas
There is a difference in the * usage when you are defining a variable and when you are using it.
定义变量和使用变量时 * 的用法有所不同。
In declaration,
在声明中,
int *myVariable;
Means a pointer to an integer data type. In usage however,
表示指向整数数据类型的指针。然而在使用中,
*myVariable = 3;
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.
意味着取消引用指针并使其指向的结构等于 3,而不是使指针等于内存地址 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!
在声明中表示一个整数的地址,或者一个指针!
This however
然而这
int someData;
pInteger = &someData;
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 指针本身(记住,指针只是它们指向的内存地址)等于 'someData' 的地址 - 所以现在 pInteger 将指向一些数据,并且可以在您尊重它时用于访问它:
*pInteger += 9000;
Does this make sense to you? Is there anything else that you find confusing?
你能理解这个吗?还有什么让你感到困惑的吗?
@Edit3:
@编辑3:
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.
表示 bar 指针等于一个整数,而不是 bar 所指向的,这是无效的。
&bar = &oof;
&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.
无效,你的也是。
Finally,
最后,
bar = oof
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.
并不意味着“bar 指向 oof 指针”。相反,这意味着 bar 指向 oof 指向的地址,所以 bar 指向 foo 指向的任何东西 - 而不是 bar 指向指向 oof 的 foo。
回答by JoshD
To declare a function that takes a pointer to an int:
声明一个带有指向 int 的指针的函数:
void Foo(int *x);
void Foo(int *x);
To use this function:
要使用此功能:
int x = 4;
int *x_ptr = &x;
Foo(x_ptr);
Foo(&x);
If you want a pointer for another type of object, it's much the same:
如果你想要一个指向其他类型对象的指针,它是一样的:
void Foo(Object *o);
void Foo(Object *o);
But, you may prefer to use references. They are somewhat less confusing than pointers:
但是,您可能更喜欢使用引用。它们比指针更容易混淆:
// pass a reference
void Foo(int &x)
{
x = 2;
}
//pass a pointer
void Foo_p(int *p)
{
*x = 9;
}
// pass by value
void Bar(int x)
{
x = 7;
}
int x = 4;
Foo(x); // x now equals 2.
Foo_p(&x); // x now equals 9.
Bar(x); // x still equals 9.
With references, you still get to change the x that was passed to the function (as you would with a pointer), but you don't have to worry about dereferencing or address of operations.
使用引用,您仍然可以更改传递给函数的 x(就像使用指针一样),但您不必担心取消引用或操作地址。
As recommended by others, check out the C++FAQLite. It's an excellent resource for this.
按照其他人的建议,查看C++FAQLite。这是一个很好的资源。
Edit 3 response:
编辑 3 回复:
bar = &foo means: Make bar point to foo in memory
bar = &foo 表示:让 bar 指向内存中的 foo
Yes.
是的。
*bar = foo means Change the value that bar points to to equal whatever foo equals
*bar = foo 表示将 bar 指向的值更改为等于 foo 等于的任何值
Yes.
是的。
If I have a second pointer (int *oof), then:
如果我有第二个指针 (int *oof),则:
bar = oof means: bar points to the oof pointer
bar = oof 表示:bar 指向 oof 指针
bar will point to whatever oof points to. They will both point to the same thing.
bar 将指向 oof 指向的任何内容。他们都将指向同一件事。
bar = *oof means: bar points to the value that oof points to, but not to the oof pointer itself
bar = *oof 表示:bar 指向的是 oof 指向的值,而不是指向 oof 指针本身
No. You can't do this (assuming bar is of type int *) You can make pointer pointers. (int **), but let's not get into that... You cannot assign a pointer to an int (well, you can, but that's a detail that isn't in line with the discussion).
不。你不能这样做(假设 bar 是 int * 类型)你可以制作指针指针。(int **),但让我们不要进入那个......你不能分配一个指向 int 的指针(好吧,你可以,但这是一个不符合讨论的细节)。
*bar = *oof means: change the value that bar points to to the value that oof points to
*bar = *oof 表示:将bar指向的值改为oof指向的值
Yes.
是的。
&bar = &oof means: change the memory address that bar points to be the same as the memory address that oof points to
&bar = &oof 意思是:把bar指向的内存地址改成和oof指向的内存地址一样
No. You can't do this because the address of operator returns an rvalue. Basically, that means you can't assign something to it.
不。你不能这样做,因为运算符的地址返回一个右值。基本上,这意味着您不能为其分配某些内容。
回答by Steve Jessop
If you want to pass a pointer-to-int into your function,
如果您想将指向 int 的指针传递到您的函数中,
Declaration of function (if you need it):
函数声明(如果需要):
void Fun(int *ptr);
Definition of function:
功能定义:
void Fun(int *ptr) {
int *other_pointer = ptr; // other_pointer points to the same thing as ptr
*other_ptr = 3; // manipulate the thing they both point to
}
Use of function:
功能使用:
int main() {
int x = 2;
printf("%d\n", x);
Fun(&x);
printf("%d\n", x);
}
Note as a general rule, that variables called Ptr
or Pointer
should never have type int
, which is what you have then in your code. A pointer-to-int has type int *
.
请注意,作为一般规则,调用的变量Ptr
或Pointer
不应具有 type int
,这就是您在代码中所拥有的。指向 int 的指针的类型为int *
。
If I have a second pointer (int *oof), then:
bar = oof means: bar points to the oof pointer
如果我有第二个指针 (int *oof),则:
bar = oof 表示:bar 指向 oof 指针
It means "make bar point to the same thing oof points to".
它的意思是“让 bar 指向 oof 指向的同一事物”。
bar = *oof means: bar points to the value that oof points to, but not to the oof pointer itself
bar = *oof 表示:bar 指向的是 oof 指向的值,而不是指向 oof 指针本身
That doesn't mean anything, it's invalid. bar
is a pointer *oof
is an int. You can't assign one to the other.
这没有任何意义,它是无效的。bar
是一个指针*oof
是一个int。您不能将一个分配给另一个。
*bar = *oof means: change the value that bar points to to the value that oof points to
*bar = *oof 表示:将bar指向的值改为oof指向的值
Yes.
是的。
&bar = &oof means: change the memory address that bar points to be the same as the memory address that oof points to
&bar = &oof 意思是:把bar指向的内存地址改成和oof指向的内存地址一样
Nope, that's invalid again. &bar
is a pointer to the bar
variable, but it is what's called an "rvalue", or "temporary", and it cannot be assigned to. It's like the result of an arithmetic calculation. You can't write x + 1 = 5
.
不,这又无效了。&bar
是一个指向bar
变量的指针,但它是所谓的“右值”或“临时”,并且不能分配给它。这就像算术计算的结果。你不能写x + 1 = 5
。
It might help you to think of pointers as addresses. bar = oof
means "make bar, which is an address, equal to oof, which is also an address". bar = &foo
means "make bar, which is an address, equal to the address of foo". If bar = *oof
meant anything, it would mean "make bar, which is an address, equal to *oof
, which is an int". You can't.
它可能会帮助您将指针视为地址。bar = oof
意思是“make bar,它是一个地址,等于oof,它也是一个地址”。bar = &foo
意思是“制作 bar,这是一个地址,等于 foo 的地址”。如果有bar = *oof
任何意义,它的意思是“制作 bar,这是一个地址,等于*oof
,这是一个 int”。你不能。
Then, &
is the address-of operator. It means "the address of the operand", so &foo
is the address of foo (i.e, a pointer to foo). *
is the dereference operator. It means "the thing at the address given by the operand". So having done bar = &foo
, *bar
is foo
.
然后,&
是操作符的地址。它的意思是“操作数&foo
的地址”,foo 的地址也是如此(即指向 foo 的指针)。*
是解引用运算符。它的意思是“操作数给出的地址处的东西”。如此做bar = &foo
,*bar
是foo
。
回答by dgnorton
To pass a pointer to an int it should be void Fun(int* pointer)
.
要将指针传递给 int 应该是void Fun(int* pointer)
.
Passing a reference to an int would look like this...
传递对 int 的引用看起来像这样......
void Fun(int& ref) {
ref = 10;
}
int main() {
int test = 5;
cout << test << endl; // prints 5
Fun(test);
cout << test << endl; // prints 10 because Fun modified the value
return 1;
}
回答by Gustavo Puma
void Fun(int *Pointer)
{
//if you want to manipulate the content of the pointer:
*Pointer=10;
//Here we are changing the contents of Pointer to 10
}
* before the pointer means the content of the pointer (except in declarations!)
* 指针前表示指针的内容(声明除外!)
& before the pointer (or any variable) means the address
& 在指针(或任何变量)之前表示地址
EDIT:
编辑:
int someint=15;
//to call the function
Fun(&someint);
//or we can also do
int *ptr;
ptr=&someint;
Fun(ptr);
回答by AndersK
void Fun(int* Pointer) -- would be called as Fun( &somevariable )
would allow you to manipulate the content of what 'Pointer' points to by dereferencing it inside the Fun function i.e.
将允许您通过在 Fun 函数中取消引用来操纵“指针”指向的内容,即
*Pointer = 1;
declaring it as above also allows you also to manipulate data beyond what it points to:
将其声明为上述还允许您操作超出其指向的数据:
int foo[10] = {0};
Fun(foo);
in the function you can then do like *(Pointer + 1) = 12; setting the array's 2nd value.
在函数中你可以像 *(Pointer + 1) = 12; 设置数组的第二个值。
void Fun(int& Pointer) -- would be called Fun( somevariable )
you can modify what Pointer references to, however in this case you cannot access anything beyond what Pointer references to.
您可以修改 Pointer 引用的内容,但是在这种情况下,您无法访问 Pointer 引用的内容之外的任何内容。
回答by t0mm13b
It might be easier for you to understand using Functionoids which are expressively neater and more powerful to use, see this excellent and highly recommended C++ FAQ lite, in particular, look at section 33.12 onwards, but nonetheless, read it from the start of that section to gain a grasp and understanding of it.
使用更简洁、更强大的 Functionoids 可能更容易让您理解,请参阅这个优秀且强烈推荐的C++ FAQ lite,特别是查看第 33.12 节以后,但尽管如此,请从该节的开头阅读以获得对它的掌握和理解。
To answer your question:
回答你的问题:
typedef void (*foobar)() fubarfn;
void Fun(fubarfn& baz){
fubarfn = baz;
baz();
}
Edit:
编辑:
&
means the reference address*
means the value of what's contained at the reference address, called de-referencing
&
表示参考地址*
表示引用地址中包含的内容的值,称为取消引用
So using the reference, example below, shows that we are passing in a parameter, and directly modify it.
所以使用参考,下面的例子,说明我们传入一个参数,直接修改它。
void FunByRef(int& iPtr){
iPtr = 2;
}
int main(void){
// ...
int n;
FunByRef(n);
cout << n << endl; // n will have value of 2
}