C String Processing

What happened to the next code fragment that the program worked - give a segmentation error. I am using gcc.

uint8_t result = 1      

InsertRow("Name","Details of work",result);     

void InsertRow(char *Name, char *Description,uint8_t Result)   
{  
   char Buffer[500];  

   if(Result==1)   
      sprintf(Buffer,"<tr><td>%s </td> <td> %s </td> <td>  %s </td></tr>",Name,Description,Result);   
} 
+3
source share
3 answers

You use the format specifier %sfor the type argument uint8_t, it should be %u, and you must specify a value unsigned intto match. This saves you from having to worry about the exact type and adjust the formatting (as commentators point out).

In addition, it is difficult for us to understand that the buffer is large enough, of course. If you have this, you can use snprinf()it to avoid it.

+7
source

Here

sprintf(Buffer,"<tr><td>%s </td> <td> %s </td> <td>  %s </td></tr>",Name,Description,Result);    

Result ( uint8_t) char

, , , , - , . . %s ,

. %d , uint8_t , int ( , ). %d, Result int ((int)Result)

+1

The result is not char *, you probably want% d for this

0
source

All Articles