string与char*转换(经vs2019测试可行)

string与char*转换

string–>char*
#include <iostream>
#include <string>
using namespace std;
int main() {
	std::string s = "hello world";
	char c[20];//不能为char* c;或char* c=NULL;提示必须初始化;
	strcpy(c, s.c_str());//strcpy(c,s.data());
	cout << c << endl;
}//正常打印
#include <iostream>
#include <string>
using namespace std;
int main() {
	std::string str = "hello world";
	char* chr = const_cast<char*>(str.c_str());
	cout << chr << endl;
}//正常打印
#include <iostream>
#include <string>
using namespace std;
int main() {
	string str = "hello world";
	char* chr = &str[0];
	cout << chr << endl;
}//正常打印
#include <iostream>
#include <string>
using namespace std;
int main() {
	std::string str = "hello world";
	const char* chr = str.c_str();
	cout << chr << endl;
}//正常打印
#include <iostream>
#include <string>
using namespace std;
int main() {
	std::string str = "hello world";
	char* chr = new char[str.length() + 1];
	strcpy(chr, str.c_str());
	cout << chr << endl;
	delete[] chr;
}
//正常打印

另:

在用vs2019测试时产生错误error C4996: ‘strcpy’: This function or variable may be unsafe. Consider using strcpy_s instead.

原因:strcpy()和strcat()函数不安全造成的溢出。

解决方法是:【项目属性】->【C++】->【预处理器】->编辑->_加入一段代码:_CRT_SECURE_NO_WARNINGS。

注意:

char* c; 
string s="1234"; 
c = s.c_str(); 

提示不能将const char*转换成char*类型;c_str()返回值类型为const char* 即只读不可修改的指针。

char* -->string

直接赋值

#include <iostream>
#include <string>
using namespace std;
int main() {
	char c[] = "this is a char array";
	const char* t = "const char";
	string s = t;
	string ss = c;
	cout << s << endl;
	cout << ss << endl;
}
//正常打印
#include <iostream>
#include <string>
using namespace std;
int main() {
	string s;
	char* p = "hello";
	s = p;
	cout << p << endl;
}//报错
//提示第6行错误:error C2440: “初始化”: 无法从“const char [6]”转换为“char *”
#include <iostream>
#include <string>
using namespace std;
int main() {
	string s;
	char p[] = "hello";
	s = p;
	cout << p << endl;
}//正常打印
#include <iostream>
#include <string>
using namespace std;
int main() {
	const char* chr = "this is a const exp";
	string s(chr, chr + strlen(chr));
	cout << s << endl;
}//正常打印
#include <iostream>
#include <string>
using namespace std;
int main() {
	string s="hello";
	char* ch = const_cast<char*>(s.c_str());
	cout << ch << endl;
}//正常打印

附: char* 和const char*

#include <iostream>
#include <string>
using namespace std;
int main() {
	const char* src = "this is a const exp";
	char* ch = const_cast<char*>(src);
	const char* dst = static_cast<const char*>(ch);
	cout << ch << endl;
	cout << dst << endl;
}//正常打印
//this is a const exp
//this is a const exp

参考几篇大佬们的文章,并用vs2019 Community进行测试。网页翻来覆去的只记得下边传送门了。

https://blog.csdn.net/grllery/article/details/89043618

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在函数定义中使用指针数组 char *s[] 来表示形参,是因为指针数组本身就是一个指向指针的指针,因此可以通过指针数组来访问原始的指针,进而访问数组中的数据。 而在函数调用中使用二维数组 char poem[4][20] 传入实参,实际上是将一个二维数组的首地址传递给了指针数组。由于二维数组的内存布局和指针数组是不同的,因此编译器可能会对二者进行不同的处理。因此,如果在函数定义中使用指针数组作为形参,那么最好也使用指针数组作为实参,这样可以确保程序的正确性。 也就是说,如果要将一个二维数组传递给一个使用指针数组作为形参的函数,需要先将二维数组转换为指针数组,然后再将指针数组作为实参传递给函数。例如,可以定义一个临时的指针数组,将二维数组的每一行的首地址赋值给指针数组的对应元素,然后将指针数组作为实参传递给函数。 修改后的代码如下所示: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> char* change(char *s[], int n); int main(void) { char poem[4][20]; int n, i; for (i = 0; i < 4; i++) { fgets(poem[i], sizeof(poem[i]), stdin); } char* ptr[4]; for (i = 0; i < 4; i++) { ptr[i] = poem[i]; } char* result = change(ptr, 4); for (i = 0; result[i] != '\0'; i++) { printf("%c", result[i]); } return 0; } char* change(char *s[], int n) { char* a = NULL; a = (char*)malloc(sizeof(char) * 10); int i; for (i = 0; i < n; i++) { a[2 * i] = s[i][0]; a[2 * i + 1] = s[i][1]; } a[2 * i] = '\0'; return a; } ``` 修改后的代码中,先定义一个指针数组 ptr,将二维数组 poem 的每一行的首地址赋值给 ptr 的对应元素。然后将 ptr 作为实参传递给函数 change(),确保程序的正确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值