我想使用指针技术将 char fruit
变量值从 Apple
更改为 Orange
请任何人帮助我解决我的问题。
这是代码。
#include <iostream>
using namespace std;
int main()
{
char fruit='Apple';
char *ptr_fruit;
ptr_fruit=&fruit;
*ptr_fruit='Orange';
cout<< fruit;
system("PAUSE");
}
最佳答案
您应该复习 C++ 的基础知识。并为 std::string 放弃 char*。不管怎样,如果你真的想使用这种方法,有一些提示:
- 为您的水果声明足够大的缓冲区
char fruit[50] = "Apple"
- 使用 strncpy 进行复制和一些临时容器。
关于c++ - 更改 char 变量值并使用指针打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31805973/