C++ 使用指针交换整数
1: 我将使用指针在主函数类型之外使用SwapInteger函数交换整数。用户输入一个数字,然后计算机将编译并将结果更改为教授指定的给定结果 我曾尝试创建一个void swapInteger函数,并输入一些代码,以查看是否交换了代码,但没有任何效果。所以我只是在主函数中添加了一些代码,但我认为这不是我们的教授想要我们做的。他确实说过“不要修改主要功能”C++ 使用指针交换整数,c++,visual-c++,C++,Visual C++,1: 我将使用指针在主函数类型之外使用SwapInteger函数交换整数。用户输入一个数字,然后计算机将编译并将结果更改为教授指定的给定结果 我曾尝试创建一个void swapInteger函数,并输入一些代码,以查看是否交换了代码,但没有任何效果。所以我只是在主函数中添加了一些代码,但我认为这不是我们的教授想要我们做的。他确实说过“不要修改主要功能” #包括 #包括 #包括 使用名称空间std; //TODO:实现“交换积分器”功能 无效交换整数(整数*第一,整数*第二) { int*pSwa
#包括
#包括
#包括
使用名称空间std;
//TODO:实现“交换积分器”功能
无效交换整数(整数*第一,整数*第二)
{
int*pSwapIntegers=第一;
第一=第二;
第二个=pSwapIntegers;
}
//不要修改主功能!
int main()
{
int first=0;
int秒=0;
int*pFirst=新int(第一个);
整数*秒=新整数(秒);
cout>首先;
cout>秒;
cout在您的swapIntegers()
内部,您正在交换指针本身,而不是它们所指向的变量的值。调用方的变量没有被更新
swapIntegers()
需要看起来更像这样:
void swapIntegers(int *first, int *second)
{
int saved = *first;
*first = *second;
*second = saved;
}
另外,您的main()
也有错误。它动态分配2个泄漏的int
变量,并且从不将用户的输入值分配给它们。交换后的最后一个“
output是从这些指针打印出值,而不是从实际交换的变量打印出值。代码不会显示预期的输出。因此,不管指令怎么说,main()
需要修改才能正常运行,如果您的教授对此有问题,请严格执行。他在给您的代码中犯了一个错误
main()
应该更像这样:
int main()
{
int first = 0;
int second = 0;
cout << "Enter the first integer: ";
cin >> first;
cout << "Enter the second integer: ";
cin >> second;
cout << "\nYou entered:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
swapIntegers(&first, &second);
cout << "\nAfter swapping:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
cout << "\nPress any key to quit.";
_getch();
return 0;
}
// Do not modify the main function!
int main()
{
int first = 0;
int second = 0;
int *pFirst = &first;
int *pSecond = &second;
cout << "Enter the first integer: ";
cin >> first;
cout << "Enter the second integer: ";
cin >> second;
cout << "\nYou entered:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
swapIntegers(&first, &second);
cout << "\nAfter swapping:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
cout << "\nPress any key to quit.";
_getch();
return 0;
}
int main()
{
int *pFirst = new int (0);
int *pSecond = new int (0);
cout << "Enter the first integer: ";
cin >> *pFirst;
cout << "Enter the second integer: ";
cin >> *pSecond;
cout << "\nYou entered:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
swapIntegers(pFirst, pSecond);
cout << "\nAfter swapping:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
delete pFirst;
delete pSecond;
cout << "\nPress any key to quit.";
_getch();
return 0;
}
这段代码是正确的。因此,是您在main()
中引入了指针的错误用法。因此,只需恢复到原始的main()
代码。然后实现swapIntegers()
正确。如说明所示。如果要修改参数,为什么不通过引用传递?如void swapinteger(int*first,int*second)
->void swapinteger(int*&first,int*&second)
?main
被无望地窃听。如果这是教授给你的代码,你就犯了错误。pFirst
与&first
没有相同的地址。pssecond
与&second
也不是相同的地址。相同的值,但不同的地址。swapInte>的一种实现gers
肯定可以在&first
和&second
处交换值,但是打印*pFirst
和*pssecond
的结果将不会显示所做的更改。事实上,它只会打印出0和0,因为这是分配和初始化这两个指针的内容。您可能不希望wap地址,它们只是副本。听起来很傻,是吧?它们是奇怪的指针。它们指向的是通过引用传递的,但是地址本身,它们就像任何其他变量一样,通过值传递。当你真的想更新函数中的指针并使更改生效时,这种区别将变得很重要完善调用函数。谢谢。我从来没有说过代码是由我的教授给出的。我在添加了一些代码后确实提到了这一点,这就是我到目前为止得到的。就像我在这里提到的:“我试着创建一个空的交换函数,并输入一些代码,看看是否交换了代码,但这没有任何作用。所以我只是在主函数中添加了一些代码,但我认为这不是我们的教授想要我们做的。他说了“不要修改主函数”
int main()
{
int *pFirst = new int (0);
int *pSecond = new int (0);
cout << "Enter the first integer: ";
cin >> *pFirst;
cout << "Enter the second integer: ";
cin >> *pSecond;
cout << "\nYou entered:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
swapIntegers(pFirst, pSecond);
cout << "\nAfter swapping:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
delete pFirst;
delete pSecond;
cout << "\nPress any key to quit.";
_getch();
return 0;
}
// Do not modify the main function!
int main()
{
int first = 0;
int second = 0;
cout << "Enter the first integer: ";
cin >> first;
cout << "Enter the second integer: ";
cin >> second;
cout << "\nYou entered:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
SwapIntegers(&first, &second);
cout << "\nAfter swapping:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
cout << "\nPress any key to quit.";
_getch();
return 0;
}