简体   繁体   English

迭代二维字符数组时打印值错误

[英]Wrong print values while iterating 2d char array

I am trying to run this code in Arduino IDE.我正在尝试在 Arduino IDE 中运行此代码。 It is printing wrong values.它正在打印错误的值。

char daysOfTheWeek[7][4] = {"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"};

for(int i=0;i<7;i++) {
    Serial.print(daysOfTheWeek[i]);
    Serial.print(" ");
}

Serial.println();

Printed values印刷值

Sun Mon TuesWed Wed ThurFri Fri Sat

I see this can be fixed by changing the array allocation as following我看到这可以通过更改数组分配来解决,如下所示

char *daysOfTheWeek[7] = {"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"};

I am fairly new to c++.我对 c++ 相当陌生。 Can anyone help me understand why this is happening?谁能帮我理解为什么会这样?

All string literals have an implicit null-terminator at the end.所有字符串文字的末尾都有一个隐式的空终止符。 So, the biggest ones of yours, the "Tues" and the "Thur" , are actually contain five characters, like so: "Tues\0" and "Thur\0" .因此,您最大的"Tues""Thur"实际上包含五个字符,例如: "Tues\0""Thur\0"

Thus, you either need to increase the dimension of the character array to [5] (resulting in char daysOfTheWeek[7][5] ), or you need to use "Tue" and "Thu" as initializers instead.因此,您要么需要将字符数组的维度增加到[5] (导致char daysOfTheWeek[7][5] ),要么需要使用"Tue""Thu"作为初始值设定项。

It may be graphical for you to use the fact that Serial.print() returns the number of bytes written , when you try to print the contents of the daysOfTheWeek in this way.当您尝试以这种方式打印daysOfTheWeek的内容时,使用Serial.print() 返回写入的字节数这一事实可能是图形化的。

Meanwhile, for example, in the g++ compiler your code should give you the error: error: initializer-string for array of chars is too long , pointing to the two problematic string literals mentioned above.同时,例如,在 g++ 编译器中,您的代码应该给您错误: error: initializer-string for array of chars is too long ,指向上面提到的两个有问题的字符串文字。

As for the Arduino part, you may want to consider using the String library type.至于 Arduino 部分,您可能需要考虑使用String库类型。

It is happening because the second dimension of the array is shorter than the longest word.这是因为数组的第二维比最长的单词短。

The code sets the second dimension up to 4. So each print statement will print up to 4 characters.代码将第二维设置为 4。因此每个打印语句将打印最多 4 个字符。 The null character, which is appended to each word, counts as one.附加到每个单词的空字符计为一个。 Therefore, some words like "Mon" will print correctly (3 characters for the word + 1 character for null).因此,像“Mon”这样的一些单词将正确打印(单词 3 个字符 + 空值 1 个字符)。 Longer words like "Thur" will print without the null characters, so you will see "Thur" and "Fri" back to back as in "ThurFri".像“Thur”这样的较长单词将打印没有空字符,因此您将看到“Thur”和“Fri”背靠背,就像在“ThurFri”中一样。 To fix this situation, increase the second dimension of the array!为了解决这种情况,增加数组的第二维!

The second option uses pointers for which the compiler knows the length of the word automatically, and therefore, the print will be correct.第二个选项使用编译器自动知道单词长度的指针,因此打印将是正确的。

There are more ways to do this and which you can read about here: https://www.geeksforgeeks.org/array-strings-c-3-different-ways-create/还有更多方法可以做到这一点,您可以在此处阅读: https ://www.geeksforgeeks.org/array-strings-c-3-different-ways-create/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM