车立方" type="application/atom+xml">

hahakubile Blog, Powered by 车立方

Welcome to hahakubile's blog, You should know him. Thanks to 车立方

Problem-printf Std String

问题

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <string>

using namespace std;

int main() {
   string a = "test";
   printf("%s/n", a);
}

编译错误

  • warning: cannot pass objects of non-POD type ‘struct std::string’ through ‘…’; callcall will abort at runtime
  • warning: format ‘%s’ expects type ‘char*’, but argument 2 has type ‘int’

原因

  • printf只能输出C语言内置的数据,而string不是内置的,只是一个扩展的类
  • &a代表的是这个字符串的存储地址,并不是指向字符串的首地址

解决

printf("%s\n", a.c_str());
  • const char *c_str();
  • c_str()提供了这样一种方法,它返回const char*类型(可读不可改)的指向字符数组的指针