使用 sprintf() 将文本复制到字符数组中 - C / Atmel studio 7
Copy text using sprintf() into char array - C / Atmel studio 7
我遇到了以下问题:
应按下开关并通过端口 A 的输入引脚将信号传输到端口 B(按计划工作)移动 1 个位置。现在程序将计算按下的按钮并将该文本打印到小型 LCD 显示屏上。
当我尝试使用 sprintf 将文本保存到数组时,我没有将任何内容写入内存位置。我究竟做错了什么?注意:我们应该使用 sprintf 作为解决方案。
#include "avr/io.h"
#include "stdint.h"
#include "lcd.h"
#include "stdio.h"
int main(void) {
unsigned char mask, presetb;
char *text[20];
unsigned char buttons;
//variable to save lcd infos
display myLCD;
//set pointer to DDRA & set to Input(0)
DDRA &= ~((1 << DDA2) | (1 << DDA3) | (1 << DDA4) | (1 << DDA5)); //sets bits 2 to 5 to 0
//set ptr to DDRB and set as output (1)
DDRB |= ((1 << DDB3) | (1 << DDB4) | (1 << DDB5) | (1 << DDB6)); //sets bits 3 to 6 to 1
//initialize LCD
lcd_init(&myLCD, &PORTD);
for (;;) {
//set pointer to PINA & read bits 2 to 5, save in 'mask' and shift number <<1
mask = PINA & ((1 << PIN2) | (1 << PIN3) | (1 << PIN4) | (1 << PIN5));
mask = mask << 1;
//set ptr to PORTB and copy shifted number
presetb = (~((1 << PIN3) | (1 << PIN4) | (1 << PIN5) | (1 << PIN6))) & (PORTB); //save bitvalues of bits 0 - 2 and 7
PORTB = (mask | presetb); //copy bits 0-2 and 7 of 'presetb' and bits 3 -6 of 'mask' to PORTB
//number of buttons pressed
buttons = 0;
if (PINB3 == 1)
buttons += 1;
if (PINB4 == 1)
buttons += 1;
if (PINB5 == 1)
buttons += 1;
if (PINB6 == 1)
buttons += 1;
//set string
sprintf(*text, "Pushed Buttons %d", (unsigned char)buttons);
//print text "Pushed Buttons [Nbr]"
lcd_send_string(&myLCD, *text, 1, 1);
}
return 0;
}
文本应该是
char text[20];
脱掉*
char *text[20];
是一个包含 20 个指向 char
的指针的数组。排队
sprintf(*text, "Pushed Buttons %d", (unsigned char) buttons);
您正在尝试使用 sprintf
将文本放入 *text
,这是数组的第一个条目,指向 char 的指针。未初始化的指针。
你要的是
char text[20];
sprintf(text, "Pushed Buttons %d", (unsigned char) buttons);
20 个字符的数组。
我遇到了以下问题: 应按下开关并通过端口 A 的输入引脚将信号传输到端口 B(按计划工作)移动 1 个位置。现在程序将计算按下的按钮并将该文本打印到小型 LCD 显示屏上。
当我尝试使用 sprintf 将文本保存到数组时,我没有将任何内容写入内存位置。我究竟做错了什么?注意:我们应该使用 sprintf 作为解决方案。
#include "avr/io.h"
#include "stdint.h"
#include "lcd.h"
#include "stdio.h"
int main(void) {
unsigned char mask, presetb;
char *text[20];
unsigned char buttons;
//variable to save lcd infos
display myLCD;
//set pointer to DDRA & set to Input(0)
DDRA &= ~((1 << DDA2) | (1 << DDA3) | (1 << DDA4) | (1 << DDA5)); //sets bits 2 to 5 to 0
//set ptr to DDRB and set as output (1)
DDRB |= ((1 << DDB3) | (1 << DDB4) | (1 << DDB5) | (1 << DDB6)); //sets bits 3 to 6 to 1
//initialize LCD
lcd_init(&myLCD, &PORTD);
for (;;) {
//set pointer to PINA & read bits 2 to 5, save in 'mask' and shift number <<1
mask = PINA & ((1 << PIN2) | (1 << PIN3) | (1 << PIN4) | (1 << PIN5));
mask = mask << 1;
//set ptr to PORTB and copy shifted number
presetb = (~((1 << PIN3) | (1 << PIN4) | (1 << PIN5) | (1 << PIN6))) & (PORTB); //save bitvalues of bits 0 - 2 and 7
PORTB = (mask | presetb); //copy bits 0-2 and 7 of 'presetb' and bits 3 -6 of 'mask' to PORTB
//number of buttons pressed
buttons = 0;
if (PINB3 == 1)
buttons += 1;
if (PINB4 == 1)
buttons += 1;
if (PINB5 == 1)
buttons += 1;
if (PINB6 == 1)
buttons += 1;
//set string
sprintf(*text, "Pushed Buttons %d", (unsigned char)buttons);
//print text "Pushed Buttons [Nbr]"
lcd_send_string(&myLCD, *text, 1, 1);
}
return 0;
}
文本应该是
char text[20];
脱掉*
char *text[20];
是一个包含 20 个指向 char
的指针的数组。排队
sprintf(*text, "Pushed Buttons %d", (unsigned char) buttons);
您正在尝试使用 sprintf
将文本放入 *text
,这是数组的第一个条目,指向 char 的指针。未初始化的指针。
你要的是
char text[20];
sprintf(text, "Pushed Buttons %d", (unsigned char) buttons);
20 个字符的数组。