gpt4 book ai didi

c - 空终止字符串显示问题

转载 作者:行者123 更新时间:2023-11-30 15:18:11 25 4
gpt4 key购买 nike

我编写了一小段代码,用于在 2x16 字符 LCD 中显示数据。

char str2[] = "Hello Welcome";
char *ptr2=str2;

initializeLCD();
sendLCDCommand(CLEAR_DISPLAY);
while(*ptr2) {
displayInRow1(ptr2++);
CL_delayMS(300);
}

在此代码中,显示屏显示“Hello Welcomeeeeeeeeeeeeeeeeeeeee”我认为 while(*ptr2) 将在打印字符串中的最后一个字母“e”后终止。如果我注释包含 while(*ptr2) 的行,数据将正确显示。我不确定上面这段代码有什么问题。我正在使用 Atmel Studio 和 Atmega328 Controller 。 displayInRow1() 函数经过测试,没有问题。

最佳答案

您的函数 displayInRow1() 接受一个指针并将其传递给 sendTextToLCD() 以显示字符串。这就是为什么当您删除 while 时它可以正常工作。所以你所需要的只是

initializeLCD();
sendLCDCommand(CLEAR_DISPLAY);
displayInRow1(str2);

我不会尝试解释您得到的结果,只是说 while 循环显示类似这样的内容

Hello Welcome
ello Welcome
llo Welcome
lo Welcome
o Welcome
Welcome
Welcome
elcome
lcome
come
ome
me
e

关于c - 空终止字符串显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31639577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com