指针功能 [英] Pointers in function
问题描述
你好
如何将指针传递给函数并将其设置为指向特定的
地址?我正在尝试在这个简单的示例代码中执行此操作,但是这个
函数不会更改指针名为counter的地址。我知道我可以简单地改变这段代码来改变计数器的价值
变量,但我想要做的是真实的是设置指针
作为函数参数传递给指向类对象。我希望我写得很清楚。
我会很乐意为你提供帮助
int func(int * counter) {
int * pToInt = new int;
* pToInt = 5;
counter = pToInt;
return 0;
}
int main(int argc,char * argv [])
{
int *测试;
func(测试);
coutu<< * test<< endl;
system(" Pause");
返回0;
}
Hello
How to pass pointer to function and set it to point on particular
address? I''m trying to do this in this simple&sample code, but this
function doesn''t change address pointed by pointer named counter. I
know that I can simple change this code to change value of the counter
variable, but what what I want to do for real is to set up pointer
passed as function argument to point on a class object. I hope I wrote
this quite clear.
I would be greatfull for your help
int func(int* counter){
int* pToInt=new int;
*pToInt=5;
counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(test);
coutu<<*test<<endl;
system("Pause");
return 0;
}
推荐答案
mangood2005写道:
mangood2005 wrote:
Hello
如何将指针传递给函数并将其设置为指向特定的地址?我正在尝试在这个简单的示例代码中执行此操作,但此功能不会更改指针名为counter的地址。我知道我可以简单地改变这段代码来改变计数器变量的值,但是我想要做的是真实的是设置指针
作为函数参数传递给指向一个类对象。我希望我写得很清楚。
我会非常感谢你的帮助
int func(int * counter){
int * pToInt = new int;
* pToInt = 5;
counter = pToInt;
返回0;
}
int main(int argc,char * argv [])
{
int * test;
func(test);
coutu<< * test<< endl;
system(" Pause");
返回0 ;
}
Hello
How to pass pointer to function and set it to point on particular
address? I''m trying to do this in this simple&sample code, but this
function doesn''t change address pointed by pointer named counter. I
know that I can simple change this code to change value of the counter
variable, but what what I want to do for real is to set up pointer
passed as function argument to point on a class object. I hope I wrote
this quite clear.
I would be greatfull for your help
int func(int* counter){
int* pToInt=new int;
*pToInt=5;
counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(test);
coutu<<*test<<endl;
system("Pause");
return 0;
}
你需要传递指针的地址:
int func (int ** counter){
int * pToInt = new int;
* pToInt = 5;
* counter = pToInt;
返回0;
}
int main(int argc,char * argv [])
{
int * test;
func(& test);
cout<< * test<< endl;
系统(暂停);
返回0;
}
或者您可以使用参考:
int func(int * &安培;柜台){
int * pToInt = new int;
* pToInt = 5;
counter = pToInt;
返回0;
}
int main(int argc,char * argv [])
{
int * test;
func(test);
cout<< * test<< endl;
system(" Pause" );
返回0;
}
- Pete
You need to pass the address of the pointer:
int func(int** counter){
int* pToInt=new int;
*pToInt=5;
*counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(&test);
cout<<*test<<endl;
system("Pause");
return 0;
}
Or you can use references:
int func(int*& counter){
int* pToInt=new int;
*pToInt=5;
counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(test);
cout<<*test<<endl;
system("Pause");
return 0;
}
- Pete
Pete C.写道:
Pete C. wrote:
mangood2005写道:
mangood2005 wrote:
你好
如何将指针传递给函数并将其设置为指向特定的地址?我正在尝试在这个简单的示例代码中执行此操作,但此功能不会更改指针名为counter的地址。我知道我可以简单地改变这段代码来改变
计数器变量的值,但是我想要做的事情就是设置
指针作为函数参数传递给指向一个类对象。我希望我写得非常清楚。
我会很乐意为你提供帮助
int func(int * counter){
int * pToInt = new int;
* pToInt = 5;
counter = pToInt;
返回0;
}
int main(int argc,char * argv [])
{
int * test;
func(test);
coutu<< * test<< endl;
system(" Pause");
返回0 ;
}
Hello
How to pass pointer to function and set it to point on particular
address? I''m trying to do this in this simple&sample code, but this
function doesn''t change address pointed by pointer named counter. I
know that I can simple change this code to change value of the
counter variable, but what what I want to do for real is to set up
pointer passed as function argument to point on a class object. I
hope I wrote this quite clear.
I would be greatfull for your help
int func(int* counter){
int* pToInt=new int;
*pToInt=5;
counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(test);
coutu<<*test<<endl;
system("Pause");
return 0;
}
你需要传递指针的地址:
int func(int ** counter){
int * pToInt = new int;
* pToInt = 5;
* counter = pToInt;
返回0;
}
int main(int argc, char * argv [])
{
int * test;
func(& test);
cout<< * test<< endl;
system (暂停);
返回0;
}
或者你可以使用引用:
int func(int * & counter){
int * pToInt = new int;
* pToInt = 5;
counter = pToInt;
返回0;
}
int main(int argc,char * argv [])
{
int * test;
func(test);
cout<< * test<< endl;
system(" Pause" ;);
返回0;
}
- Pete
You need to pass the address of the pointer:
int func(int** counter){
int* pToInt=new int;
*pToInt=5;
*counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(&test);
cout<<*test<<endl;
system("Pause");
return 0;
}
Or you can use references:
int func(int*& counter){
int* pToInt=new int;
*pToInt=5;
counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(test);
cout<<*test<<endl;
system("Pause");
return 0;
}
- Pete
顺便说一句,你需要记住删除你的指针。每个新的必须有相应的删除,每个新的[]都必须删除[]。
- Pete
By the way, you need to remember to delete your pointer. Every new must have
a corresponding delete, every new[] must have a delete[].
- Pete
" Pete C." < x@x.x>在消息中写道
新闻:%k **************** @ newsread1.news.pas.earthli nk.net ...
"Pete C." <x@x.x> wrote in message
news:%k****************@newsread1.news.pas.earthli nk.net...
mangood2005写道:
你需要传递指针的地址:
int func(int ** counter){
mangood2005 wrote:
You need to pass the address of the pointer:
int func(int** counter){
使用两个星号是什么意思?我有一个程序工作,传递
指针,其中参数是这样的,
(int * iscreenX)
和其他所有内容都与你的方式相同。你知道
有什么区别?我真的很好奇,在使用Java之后,我对C ++相对较新,但是我已经习惯了指针。
谢谢
Nick howes
What does it mean to use two asterisks? I have a program working that passes
pointers where the argument is like this,
(int *iscreenX)
and everything else is otherwise the same to how you did it. do you know
what the difference is? I''m just curious really, I''m relatively new to C++
after using Java but i''m getting used to pointers.
thanks
Nick howes
这篇关于指针功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!