64,649
社区成员
发帖
与我相关
我的任务
分享
const char *temp = string.c_str();
for(i循环){
const char *temp = string[i].c_str();
cout<<sizeof(temp)<<endl;
}
为什么我用 sizeof(temp)求长度时输出的都是第一个temp的长度?
const char *temp = string.c_str();//这个本身就有问题,
string.c_str()返回的是一个字符串,要定义一个字符数组来接收才行
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
string str[2];
str[0] = "csdn.net";
str[1] = "helloworld!!!";
for(int i = 0; i < 2; ++i)
{
char tmp[256];
strcpy(tmp, str[i].c_str());
cout << sizeof(tmp) << endl;
}
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
void main()
{
string s = "abcdefg";
char arr[7];
for(int i=0;i<7;i++)
arr[i]=s[i];
for(i=0;i<7;i++)
cout<<arr[i];
cout<<endl;
}