Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4730048
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: C/C++

2010-08-31 10:25:24

#include

#include
using namespace std;
int main()
{
   string a;
   a[0]='a';
   a[1]='\0';
   printf("%s\n",a);
   system("pause");
}

出错: [Warning] cannot pass objects of non-POD type `struct std::string' through `...'; call will abort at runtime

printf只能输出C语言内置的数据,而string不是内置的,只是一个扩展的类,这样肯定是链接错误的。string不等于char*,&a代表的是这个字符串的存储地址,并不是指向字符串的首地址,aa    对象中包含一个指向"string"的指针, &aar得到的是这个对象的地址,不是"string"的地址。

printf输出string类型应如此操作!

#include
#include
using namespace std;


void main()
{
string aa="qqq";
printf("%s",aa.c_str()); //不推荐

//或者cout<}

由于string是C的一个 扩展类型,其数据赋值可以用其提供的方法:assign(const char *)或直接用其构造函数      
string str( "Now is the time..." );

 

 

阅读(3296) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~