求助个有关字符串的问题
c++吧
全部回复
仅看楼主
level 7
失重了吧
楼主
#include <iostream>
#include<string>
using namespace std;
int main() {
char *a="asdfgh";
char *b="qwersdds";
cout<< strncpy(a,b,5);
return 0;
}
运行为什么会出错??
2012年08月09日 14点08分
1
level 12
贴吧用户_0JNJWtE684
char *a= -> char a[] =
2012年08月09日 14点08分
2
失重了吧
为什么呀?
2012年08月09日 14点08分
失重了吧
直接这么写运行也会出错
#include <iostream> #
include using namespace std; int main() { //char *a="asdfgh"; //char *b="qwer"; cout<< strncpy("sadsad","bdxfsdf",5); system("pause"); }
2012年08月09日 14点08分
笑爰人03
回复 失重了吧 :先去学好C语言吧,再学C++,这是C语言问题,你说常量值能赋值吗...
2012年08月09日 14点08分
贴吧用户_0JNJWtE684
回复 失重了吧 :字符串字面量代表的值的类型是const char*/const wchar_t*,不允许修改。
2012年08月09日 14点08分
level 7
贴吧用户_0aX44SZ
char* 的a、b指向的字符串是在不可修改的常量区里。所以修改是非法的。
2012年08月09日 14点08分
3
level 6
打酱油的路过飘
打上“”的都是字符串常量,是不可修改的。如果要衔接的话,只能用数组。
2012年08月09日 16点08分
4
level 7
at1992127
不知道有没有写反。。。。。。。。。。。 新手勿喷。。。
2012年08月09日 17点08分
5
level 15
幻の上帝
C语言中字符串字面量的类型是非const数组类型,修改导致UB。
C++中字符串字面量的类型是const数组类型,去掉const修改还是UB。
至于非const指针指向字面量是C++98/03为了兼容旧代码的deprecated的array-to-pointer conversion的特例。C++11中非法。
2012年08月10日 01点08分
7
1