关于指针的问题? [英] Question about pointers?

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

问题描述

我的印象是变量是存储在内存中的
地址和指针是变量的内存地址?


我正在寻找使用一个不返回值的函数void

无论什么(无论如何)并且没有变量全局但是在main中并且更改了

值无论什么功能都无效。


使用命名空间std;

无论什么(int);


int main()

{

int test;

test = 1;

无论如何(测试);

返回1;

}

无效(int测试)

{

& test =测试

//以上一行我不确定如何做到这一点。

//我需要更改才能更改

//在main中测试的值虽然是什么函数

//但实际上不需要返回一个值。

}

谢谢你,

Shawn Mulligan

I am under the the impression that a variable is what is stored in a memory
address and a pointer is the memory address of the variable?

I was looking to use a function that does not return a value void
whatever(whatever) and not have a variable global but in main and change the
value in the void whatever function.

using namespace std;
void whatever(int);

int main()
{
int test;
test = 1;
whatever(test);
return 1;
}
void whatever(int testing)
{
&test = testing
// The above line I am unsure of how to exactly do it.
//what would I need to change in order to change the
//value of test in main though the whatever function
//without actually having to return a value.
}
Thank you,
Shawn Mulligan

解决方案

kazack写道:
我的印象是变量是存储在内存中的地址和指针是内存变量的地址?

我当时想要使用一个不返回值的函数void
无论什么(无论如何)并且没有变量全局但是在main中并且更改
无效函数中的值。

使用命名空间std;
无效(int);

int main()
{
int test;
test = 1;
无论如何(测试);
返回1;
}
无效(int测试)


只需进行以上操作:

无效(int&测试)


对测试所做的任何更改都会反映在来电者身上。

{
& test =测试
//上面这行我不确定如何准确地做到这一点。
//我需要改变什么才能改变主要的测试值,而不管函数是什么? br /> //实际上没有必要返回一个值。
}
I am under the the impression that a variable is what is stored in a memory
address and a pointer is the memory address of the variable?

I was looking to use a function that does not return a value void
whatever(whatever) and not have a variable global but in main and change the
value in the void whatever function.

using namespace std;
void whatever(int);

int main()
{
int test;
test = 1;
whatever(test);
return 1;
}
void whatever(int testing)
Just make the above:
void whatever(int& testing)

and any changes made to `testing'' will be reflected in the caller.
{
&test = testing
// The above line I am unsure of how to exactly do it.
//what would I need to change in order to change the
//value of test in main though the whatever function
//without actually having to return a value.
}




....和*请*在你的查找'引用''最喜欢的C ++文档。


HTH,

- g

-

Artie Gold - - 德克萨斯州奥斯汀

哦,对于常规老垃圾邮件的美好时光。



....and *please* look up `reference'' in your favorite C++ documentation.

HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.


kazack写道:
kazack wrote:
我的印象是变量是存储在
内存地址中的,而指针是变量的内存地址?


或多或少。您可能宁愿说变量必须位于内存中的某个位置,并且每个内存都有一个地址。一个指针可以持有

这样的地址。

我想要使用一个不返回值的函数void
无论什么(无论如何)并且没有变量全局但在main中
改变void中的值无论函数如何。

使用namespace std;
void(int);

int main()
{} int test;
test = 1;
无论如何(测试);
返回1;
}
无效(int测试)
{
& test =测试
//以上几行我不确定如何做到这一点。
//我需要改变什么为了改变主要的测试值,不管函数是什么//实际上不需要返回一个值。
I am under the the impression that a variable is what is stored in a
memory address and a pointer is the memory address of the variable?
More or less. You could rather say that a variable must be somewhere in
memory, and every piece of memory has an address. A pointer can hold
such an address.
I was looking to use a function that does not return a value void
whatever(whatever) and not have a variable global but in main and
change the value in the void whatever function.

using namespace std;
void whatever(int);

int main()
{
int test;
test = 1;
whatever(test);
return 1;
}
void whatever(int testing)
{
&test = testing
// The above line I am unsure of how to exactly do it.
//what would I need to change in order to change the
//value of test in main though the whatever function
//without actually having to return a value.




你无法通过这种方式改变变量''test'',因为它是按值传递给函数的
,即它的副本,以及你的

whatever()函数只能看到该副本。对

''测试''参数的任何修改都只会修改函数中的本地副本。

为了做你想做的事,你需要通过引用传递,即:


无效(int& testing)

{

//对测试做一些修改

}


这样,测试将是一个参考,这意味着它只是

变量''test'的另一个名字来自main(),所以任何更改为

''测试''在任何()中将导致在main()中更改''test''。



You cannot change the variable ''test'' from main this way, because it is
passed to the function by value, i.e. a copy of it is made, and your
whatever() function only sees that copy. Any modification to the
''testing'' parameter will only modify the local copy in the function.
For doing what you want to do, you need to pass by reference, i.e.:

void whatever(int& testing)
{
//do some changes to testing
}

This way, testing will be a reference, which means that it is just
another name for the variable ''test'' from main(), so any change to
''testing'' in whatever() will result in a changed ''test'' in main().


无论如何也可以使用指针引用一个

变量,这不是全局的,不会直接传递给函数吗?


或者它必须是全球性的还是通过的?


谢谢你,

Shawn Mulligan
Is there anyway possible also with the use of pointers to reference a
variable, that is not global and is not passed directly into a function?

Or does it have to be global or passed?

Thank You,
Shawn Mulligan

这篇关于关于指针的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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