[英]How to create an input method in C++ with pointers
I want to create an input method that creates two ints with Pointers.我想创建一个输入法,用指针创建两个整数。 It would be nice if you could help me or give me any tips.
如果您能帮助我或给我任何提示,那就太好了。 :)
:)
my method:我的方法:
void inputTest(int* x, int* y) {
cin >> x;
cin >> y;
}
my main:我的主要:
int *x = 0;
int *y = 0;
cout << "Input: " << endl;
//set input from user x,y with input method
inputTest(x,y);
First of all, you want to read int
s, not int*
s, so you need to dereference the pointers:首先,您要读取
int
s,而不是int*
s,因此您需要取消引用指针:
void inputTest(int* x, int* y) {
cin >> *x;
cin >> *y;
}
Then you need to pass valid pointers to the function - yours are null pointers and point nowhere at all.然后你需要将有效指针传递给 function - 你的是null 指针并且根本没有指向任何地方。
The best way to do this is to first create two int
s and then acquire their locations with the "address-of" operator, &
.最好的方法是首先创建两个
int
,然后使用“address-of”运算符&
获取它们的位置。
int x = 0;
int y = 0;
cout << "Input: " << endl;
inputTest(&x, &y);
I'm new to pointers so I want to try it that way.. :)
我是指针的新手,所以我想这样尝试.. :)
Ok, then first lesson: Do not use pointers when you don't have to.好的,那么第一课:不要在不必要的时候使用指针。 Pointers can cause the most nasty bugs that you dont get without them.
指针可能会导致最讨厌的错误,如果没有它们,您将无法获得这些错误。
Next: Pointers are just pointers. Next: 指针只是指针。 The can point to something.
罐头指向某物。 A
int*
can point to an int
.一个
int*
可以指向一个int
。 Your pointers do not point to anything meaningful.你的指针没有指向任何有意义的东西。
To store integer values you need int
s somewhere.要存储 integer 值,您需要
int
某处。 Having pointers pointing somewhere is not sufficient.有指向某处的指针是不够的。 Once you have a
int
, eg int x;
一旦你有一个
int
,例如int x;
then &x
will give you a int*
namely the address of x
( &
is called the address-of operator, but dont get confused, &
can have a different meaning, see below).然后
&x
会给你一个int*
即x
的地址( &
被称为地址运算符,但不要混淆, &
可以有不同的含义,见下文)。 If you have the pointer, int* p = &x;
如果你有指针,
int* p = &x;
then you can dereference the pointer to get back x: *p = 5;
然后你可以取消引用指针来取回 x:
*p = 5;
will set the value of x
to 5
.将
x
的值设置为5
。 Using that you could write使用它你可以写
void inputTest(int* x, int* y) {
std::cin >> *x;
std::cin >> *y;
}
int main() {
int x,y;
inputTest(&x,&y);
std::cout << x << " " << y;
}
BUT (would like to make it even more bold, because it really is a big "but"). BUT (想让它更加大胆,因为它确实是一个很大的“但是”)。 There is an alternative and this is what you should use here.
有一个替代方案,这就是你应该在这里使用的。 Pointers as parameters are useful when "not pointing anywhere" is an allowed parameter.
当“不指向任何地方”是允许的参数时,指针作为参数很有用。 For a fucntion that wants to read input from user and store that somewhere an invalid pointer is of little use.
对于想要从用户读取输入并将其存储在某个地方的函数来说,无效指针几乎没有用处。 Better is to disallow such invalid input and use references:
更好的是禁止此类无效输入并使用引用:
void inputTest(int& x, int& y) {
std::cin >> x;
std::cin >> y;
}
int main() {
int x,y;
inputTest(x,y);
std::cout << x << " " << y;
}
I feel a bit bad for writing this answer, because when you are completely new to pointers, reading an answer here will not be enough to get a proper understanding.我觉得写这个答案有点不好,因为当你对指针完全陌生时,在这里阅读答案不足以得到正确的理解。 Get a book and read it.
拿一本书读一读。
You need to dereference the pointer in order to assign a value to the pointed location in memory.您需要取消对指针的引用,以便将值分配给 memory 中的指向位置。
void inputTest(int* xptr, int* yptr) {
cin >> *xptr;
cin >> *yptr;
}
int* x = 0
creates a pointer to location 0 in memory. int* x = 0
创建指向 memory 中位置 0 的指针。 Instead, we want to allocate memory, and then point to that memory.相反,我们要分配 memory,然后指向那个 memory。 We can initialize x as
int x = 0
and then get a pointer to it by using &x.我们可以将 x 初始化为
int x = 0
,然后使用 &x 获取指向它的指针。
int x = 0;
int y = 0;
cout << "Input: " << endl;
inputTest(&x,&y);
You can also use references...您还可以使用参考...
void inputTest(int& x, int& y) {
cin >> x;
cin >> y;
}
int x = 0;
int y = 0;
cout << "Input: " << endl;
inputTest(x,y);
You may choose one of two ways:您可以选择以下两种方式之一:
Declare your variables as regular int
s and pass their addresses to the function inputTest()
which is the recommended way.将您的变量声明为常规
int
并将它们的地址传递给 function inputTest()
这是推荐的方式。
Declare your variables as pointers to int
s and allocate valid memory to them.将变量声明为指向
int
的指针,并为它们分配有效的 memory。
Example for approach 1:方法 1 的示例:
int x = 0;
int y = 0;
cout << "Input: " << endl;
//set input from user x,y with input method
inputTest(&x, &y);
Example for approach 2:方法 2 的示例:
int *x = new int;
int *y = new int;
cout << "Input: " << endl;
//set input from user x,y with input method
inputTest(x,y);
and don't forget to release/delete the allocated memory when you're done.完成后不要忘记释放/删除分配的 memory。
Also you should use:你也应该使用:
cin >> *x;
cin >> *y;
inside your function.在你的 function 里面。
With int *x = 0
you have only declared a pointer which points to nowhere.使用
int *x = 0
您只声明了一个指向无处的指针。 And pointer shall store only 'pointing' information, not a data itself.并且指针应仅存储“指向”信息,而不是数据本身。
void inputTest(int* x, int* y) {
cin >> *x; // dereference a pointer to get to the 'real' variable
cin >> *y; // this is also a disadventage of pointers, pointer can point to nowhere,
// and your programwould crach in that situation
}
int main()
{
int x = 0; // declare a normal variable, instead of a pointer
int y = 0;
cout << "Please insert an input: " << endl;
inputTest(&x, &y); // pass addresses that point to your variables
cout << "Your input: " << x << " " << y << endl;
}
You can also achive this same functionality with references.您还可以通过引用实现相同的功能。 Maybe you will like them.
也许你会喜欢他们。
void inputTest(int& x, int& y) // mind & instead of *
{
cin >> x; // no need of dereferencing,
cin >> y;
}
int main()
{
int x = 0; // a normal variable
int y = 0;
cout << "Please insert an input: " << endl;
inputTest(x, y); // simple pass your variables
cout << "Your input: " << x << " " << y << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.