This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

scanf problem

Hi, im trying to do an SMS project using AT Commands , i wanna display what the mobile sends on LCD , something like when i write the correct AT command using printf("AT\r"); it gives OK , i wanna display that OK on LCD , so i used scanf to capture the message from the mobile but it doesnt work, it freezes the display.
here is my code so far


char array[20];
lcd_com(0XC0);
lcd_puts("SMS Project");
DELAY_Second();
DELAY_Second();
DELAY_Second();
DELAY_Second();
DELAY_Second();
DELAY_Second();
lcd_com(0x01);
while(1){

 printf("AT\r");
 scanf("%s",&array);
 PC_LINK_IO_Send_String(&array);
 lcd_puts(array);


}


void lcd_puts(char *aaa){
unsigned int i=0;
for(;aaa[i]!=0;i++)lcd_data(aaa[i]);
}
}


Parents
  • Careful with scanf! It can be lethal - especially when scanning for strings. Can you tell me how scanf() would be able to know the size of your array, and what would happen if the received data doesn't fit?

    In Keil you can use gets() to get a text line. For most other environments, you should instead look for fgets(). It is important that always (!) make sure that your program can't overwrite any buffers.

    By the way - you do know that the name of an array is the same as the pointer to the first element in the array?

    Another thing: What would happen if you manage to call lcd_puts() with a malformed string not containing a terminating zero? How many characters would then be written?

Reply
  • Careful with scanf! It can be lethal - especially when scanning for strings. Can you tell me how scanf() would be able to know the size of your array, and what would happen if the received data doesn't fit?

    In Keil you can use gets() to get a text line. For most other environments, you should instead look for fgets(). It is important that always (!) make sure that your program can't overwrite any buffers.

    By the way - you do know that the name of an array is the same as the pointer to the first element in the array?

    Another thing: What would happen if you manage to call lcd_puts() with a malformed string not containing a terminating zero? How many characters would then be written?

Children