Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C+中使用指针进行字符串处理+; 我正在学习C++的基础知识。代码工作正常,正如我所希望的,但我想知道的是,在案例2中,函数用于交换字符串,基本上发生了什么?数组[pos1-1]中的字符串是否被复制到temp然后交换,或者只是要交换的字符串的地址被重新分配 #include <iostream> using namespace std; int main() { const char *array[] {"Albert", "Newton", "Gallilio", "Hawking"}; cout << "What do you want to do:\n1-Display Strings\n2-Swap Positions" << endl; unsigned short int choice{}; cin >> choice; switch (choice) { case 1: { for (short int i = 0; i < _countof(array); i++) { cout << "\n" << array[i]; } cout << endl; break; } case 2: { short int pos1{}, pos2{}; const char *temp{ nullptr }; cout << "Whom do you want to swap? Enter two numbers when prompted: " << endl; cout << "Swap --- "; cin >> pos1; cout << "With --- "; cin >> pos2; temp = array[pos1 - 1]; array[pos1 - 1] = array[pos2 - 1]; array[pos2 - 1] = temp; cout << "\nChanged Order is"; for (short int i{} ; i < _countof(array); i++) { cout << "\n" << array[i]; } cout << endl; break; } default: cout << "\a"; } return 0; } #包括 使用名称空间std; int main() { 常量字符*数组[]{“Albert”, “牛顿”, “加里里奥”, “霍金”}; 不能选择; 开关(选择) { 案例1: { 对于(短int i=0;i_C++_Pointers_Swap - Fatal编程技术网

在C+中使用指针进行字符串处理+; 我正在学习C++的基础知识。代码工作正常,正如我所希望的,但我想知道的是,在案例2中,函数用于交换字符串,基本上发生了什么?数组[pos1-1]中的字符串是否被复制到temp然后交换,或者只是要交换的字符串的地址被重新分配 #include <iostream> using namespace std; int main() { const char *array[] {"Albert", "Newton", "Gallilio", "Hawking"}; cout << "What do you want to do:\n1-Display Strings\n2-Swap Positions" << endl; unsigned short int choice{}; cin >> choice; switch (choice) { case 1: { for (short int i = 0; i < _countof(array); i++) { cout << "\n" << array[i]; } cout << endl; break; } case 2: { short int pos1{}, pos2{}; const char *temp{ nullptr }; cout << "Whom do you want to swap? Enter two numbers when prompted: " << endl; cout << "Swap --- "; cin >> pos1; cout << "With --- "; cin >> pos2; temp = array[pos1 - 1]; array[pos1 - 1] = array[pos2 - 1]; array[pos2 - 1] = temp; cout << "\nChanged Order is"; for (short int i{} ; i < _countof(array); i++) { cout << "\n" << array[i]; } cout << endl; break; } default: cout << "\a"; } return 0; } #包括 使用名称空间std; int main() { 常量字符*数组[]{“Albert”, “牛顿”, “加里里奥”, “霍金”}; 不能选择; 开关(选择) { 案例1: { 对于(短int i=0;i

在C+中使用指针进行字符串处理+; 我正在学习C++的基础知识。代码工作正常,正如我所希望的,但我想知道的是,在案例2中,函数用于交换字符串,基本上发生了什么?数组[pos1-1]中的字符串是否被复制到temp然后交换,或者只是要交换的字符串的地址被重新分配 #include <iostream> using namespace std; int main() { const char *array[] {"Albert", "Newton", "Gallilio", "Hawking"}; cout << "What do you want to do:\n1-Display Strings\n2-Swap Positions" << endl; unsigned short int choice{}; cin >> choice; switch (choice) { case 1: { for (short int i = 0; i < _countof(array); i++) { cout << "\n" << array[i]; } cout << endl; break; } case 2: { short int pos1{}, pos2{}; const char *temp{ nullptr }; cout << "Whom do you want to swap? Enter two numbers when prompted: " << endl; cout << "Swap --- "; cin >> pos1; cout << "With --- "; cin >> pos2; temp = array[pos1 - 1]; array[pos1 - 1] = array[pos2 - 1]; array[pos2 - 1] = temp; cout << "\nChanged Order is"; for (short int i{} ; i < _countof(array); i++) { cout << "\n" << array[i]; } cout << endl; break; } default: cout << "\a"; } return 0; } #包括 使用名称空间std; int main() { 常量字符*数组[]{“Albert”, “牛顿”, “加里里奥”, “霍金”}; 不能选择; 开关(选择) { 案例1: { 对于(短int i=0;i,c++,pointers,swap,C++,Pointers,Swap,让我们假设pos1是1,pos2是2 然后,array[pos1-1]保存一个指向字符a的指针,该字符被解释为以null结尾的字符串Albert\0。同样,array[pos2-1]保存一个指向字符串Newton\0的指针 当点击case2时,temp被初始化为保存空指针。然后temp被分配为保存指向Albert\0的指针副本。然后array[pos1-1]被重新分配为保存指向Newton\0的指针。然后我们需要array[pos2-1]指向Albert\0,但是数组[pos1-1]中的指针值不

让我们假设
pos1
是1,
pos2
是2

然后,
array[pos1-1]
保存一个指向字符
a
的指针,该字符被解释为以null结尾的字符串
Albert\0
。同样,
array[pos2-1]
保存一个指向字符串
Newton\0
的指针


当点击
case2
时,
temp
被初始化为保存空指针。然后
temp
被分配为保存指向
Albert\0
的指针副本。然后
array[pos1-1]
被重新分配为保存指向
Newton\0
的指针。然后我们需要
array[pos2-1]
指向
Albert\0
,但是
数组[pos1-1]
中的指针值不再指向那里。这就是临时副本的作用,因为它仍然是指向
Albert\0
的指针,所以此指针被复制到
数组[pos2-1]

后者;
temp
array
的元素都是指针,因此交换操作只是交换字符串的地址。欢迎使用StackOverflow。您应该更喜欢
std::string
而不是使用
char
的数组作为字符串。这样更易于处理,也更不容易出错(例如关于缓冲区溢出、内存管理等)。明白了!谢谢各位!帮了大忙!非常感谢!