关于 vc++ char* 的问题
inta=4;char*b[]={"1","2","3","4","5"};现在我想要把a转化成字符形并且赋值给b[]的第一个字符串,使其变成{"4","2","3","...
int a=4;
char * b[]={"1","2","3","4","5"};
现在我想要把a转化成字符形并且赋值给b[]的第一个字符串,使其变成{"4","2","3","4","5"},应该怎么做呢?
我试过这么做,但是报错了:
_itoa(a,b[0],10); 展开
char * b[]={"1","2","3","4","5"};
现在我想要把a转化成字符形并且赋值给b[]的第一个字符串,使其变成{"4","2","3","4","5"},应该怎么做呢?
我试过这么做,但是报错了:
_itoa(a,b[0],10); 展开
4个回答
展开全部
char * b[]={"1","2","3","4","5"};
相当于 char * b[5]={"1","2","3","4","5"};
就是定义5个指针,分别指向5个字符串"1","2","3","4","5",
我们知道这个五个字符串是常量,放在程序的静态缓冲区,是只读的。不可以修改只读的内存区。
就像 char*p = "hello word"; p[0] = 'H';//这样做是非法的
你的代码可以改成:
char a[5][2]={"1","2","3","4","5"};//不是静态内存
char * b[]={a[0],a[1],a[2],a[3],a[4]};
int a=4;
b[0][0] = '0'+a;
相当于 char * b[5]={"1","2","3","4","5"};
就是定义5个指针,分别指向5个字符串"1","2","3","4","5",
我们知道这个五个字符串是常量,放在程序的静态缓冲区,是只读的。不可以修改只读的内存区。
就像 char*p = "hello word"; p[0] = 'H';//这样做是非法的
你的代码可以改成:
char a[5][2]={"1","2","3","4","5"};//不是静态内存
char * b[]={a[0],a[1],a[2],a[3],a[4]};
int a=4;
b[0][0] = '0'+a;
展开全部
最好用
char b[5][10] = {"1","2","3","4","5"};
itoa(a,*(char*(&b[0])),10);
或者
char b[5] = {'1','2','3','4','5'};
itoa(a,b[0],10);
char b[5][10] = {"1","2","3","4","5"};
itoa(a,*(char*(&b[0])),10);
或者
char b[5] = {'1','2','3','4','5'};
itoa(a,b[0],10);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
试一下,
CString s;
s.Format(" %s ",a);
再把s赋值进去
CString s;
s.Format(" %s ",a);
再把s赋值进去
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不能用指针数组,如果你这么赋初值就相当于让指针指向"4","2","3"这样的字符串常量,常量当然不允许修改了。
只能用字符数组解决这个问题。
只能用字符数组解决这个问题。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询