无法在C ++中的二维数组中显示字符串

时间:2014-05-30 11:07:14

标签: c++ string

我正在开发一个应用程序,我必须为变量分配一个特定的值。

tU8 u8ADRPartData = 10;
char TunerArray[38][30] = {{0,"reserved"},
                            {1,"TunVar1 2FE DDA"},
                            {2,"TunVar2 1FE"},
                            {3,"TunVar3 2FE"},
                            {4,"reserved"},
                            {5,"TunVar5 3FE DDA"},
                            {6,"TunVar6 3FE DAB"},
                            {7,"TunVar7 4FE DDA DAB"},
                            {8,"TunVar8 2FE HD"},
                            {9,"TunVar9 2FE DDA HD"},
                            {10,"TunVar10 3FE DDA HD"},
                            {11,"TunVar11 3FE DAB SBM"},
                            {12,"TunVar12 4FE DDA DAB SBM"},
                            {13,"TunVar13 2FE DDA DDS"},
                            {14,"TunVar14 3FE DDA DDS DAB"},
                            {15,"TunVar15 3FE DDA DDS DAB"},
                            {16,"TunVar16 2FE"},
                            {17,"TunVar17 3FE DAB"},
                            {18,"TunVar18 2FE DDA DDS"},
                            {19,"TunVar19 2FE DDA DDS"},
                            {20,"TunVar20 3FE DDA DDS DAB"},
                            {21,"TunVar21 3FE DDA DDS DAB"},
                            {22,"TunVar22 3FE DDA DDS DAB"},
                            {23,"---"},
                            {24,"---"},
                            {25,"---"},
                            {26,"---"},
                            {27,"---"},
                            {28,"---"},
                            {29,"---"},
                            {30,"---"},
                            {31,"---"},
                            {32,"TunVar32 2FE DDA"},
                            {33,"TunVar33 3FE DDA DAB"},
                            {34,"TunVar34 2FE DAB"},
                            {35,"TunVar35 2FE DDA DDS"},
                            {36,"TunVar36"},
                            {37,"TunVar37 3FE DDA DDS DAB"}};

tString sTunerValue = TunerArray[u8ADRPartData][0];
cout<<"DISPLAY - "<<sTunerValue;

我传递了相应的tu8值(10),以确保sTunerValue显示&#34; TunVar10 3FE DDA HD&#34;。确切地说,我可以传递0到37之间的任何数字,并存储相应的字符串以显示。

我在某处错了.....它说:"error : invalid conversion from 'const char*' to 'char' [-fpermissive] {37,"TunVar37 3FE DDA DDS DAB"}};

请帮助:)

1 个答案:

答案 0 :(得分:1)

您正在使用像地图一样的数组。 TunerArray[38]中的每个条目都是大小为30的字符数组。当您为初始化提供一些值时,它们将自动按顺序排列。你应该使用

char TunerArray[38][30]; //declare
strcpy(TunerArray [0], "reserved");
////...insert other entries

char TunerArray[38][30] = { "reserved", .../*other entries*/}; //it will start to place the strings in order, from 0 to 37. 

您看到的错误,因为,您试图插入0,1 ..等,其中需要字符文字,并将0,1 ..等转换为等效字符。但是你不能将字符放在字符指针数组中,因此错误。

如果使用数组不是强制性的,我建议使用std::vector<std::string>,它会让您的生活变得更简单。