首页| 论坛| 消息

标题:【提问】在QT中的字符数组的初始化问题(C/C++)
作者:running
日期:2005-09-05 20:39
内容:

程序:
------------------------------
char array[2]={"X"};
--其他程序--
array={"X"};
------------------------------
我的思想是先给array[2]赋初值,在经过--其他程序--处理后,array[2]的初值已发现变化,我想重新给它赋值{"X"},但是老是出错;
编译如下:
# make
g++ -c -pipe -Wall -W -O2 -march=i386 -mcpu=i686 -g -DGLX_GLXEXT_LEGACY -fno-use-cxa-atexit -fno-exceptions-DQT_NO_DEBUG -I/usr/lib/qt-3.1/mkspecs/default -I. -I/usr/lib/qt-3.1/include -I.ui/ -I.moc/ -o .obj/main.o main.cpp
main.cpp: In function `int main(int, char**)':
main.cpp:9: invalid conversion from `const char*' to `char'
main.cpp:9: invalid conversion from `const char*' to `char'
main.cpp:9: invalid conversion from `const char*' to `char'
main.cpp:10: parse error before `]' token
main.cpp:7: warning: unused parameter `int argc'
main.cpp:7: warning: unused parameter `char**argv'
main.cpp: At global scope:
main.cpp:11: `argc' was not declared in this scope
main.cpp:11: `argv' was not declared in this scope
main.cpp:13: syntax error before `.' token
main.cpp:15: syntax error before `.' token
main.cpp:16: syntax error before `.' token
main.cpp:17: syntax error before `.' token
main.cpp:18: syntax error before `.' token
make: *** [.obj/main.o] Error 1
怎样给字符数组赋值啊?是不是字符在QT中的处理与其他地方的C/C++不同啊?


#1 [XChinux 09-05 21:56]
不是Qt的问题,而是你对C/C++的掌握程度不够。
给char array[2]赋值,应该使用
char array[2] = {'X', '\0'};
或者
char array[2] = "X";
的方式。
#2 [running 09-06 09:09]
我先给字符数组array赋初值,在经过--其他程序--处理后,array的初值已发现变化,即已不是{"X"}了,我想重新给赋值array={"X"},这样要出错的.怎样写呢?
#3 [XChinux 09-06 09:13]
sprintf,或者
array[0] = 'X';
array[1] = '\0';
#4 [running 09-06 09:32]
谢谢老大了 我都搞定了 非常小问题 见笑了 这几天心情不大好 思路不好
#5 [gongyh 09-06 10:26]
define char typeuse ' '
char c = ' ' ;
char c[]={'a','b'}
ok!

回复 发表
主题 版块