Hello, I'm trying to implement a code which copies a part of a string which is pointed to by a char (*outr_message), and adds it to an unsigned-char (UART1_error_format).
For example: if *outr_message is "e97" so UART1_error_format will be err97.
the current code is:
void Serial_Output_Full_Reset(char *outr_message) { unsigned char UART1_error_format[5] = "err"; //prepare the message set_message_in_transbuffer(String_Message, outr_message, NULL, NULL, 0, 0); #if __RS485__ if (&outr_message[1] == 'e') // converting 'eXX' to 'errXX' { strncpy(UART1_error_format, outr_message + 2, 2); UART1_error_format[5] = '\0'; random_write(0 ,0 , nooutr, UART1_error_format, 2); } #endif }
after compiling there's an error appearing:
new_meas_proc.c(52): warning C214: 'Argument': conversion: pointer to non-pointer
I've tried several ways to fix the warning, which is connected to the strncpy function I believe, since I've entered to the function 'random_write' correct variables types:
void random_write(float Treal, float Emiss, unsigned char Outr,unsigned char Err, unsigned char Scale);
What should be done to fix the warning? is a conversion from char to unsigned char also needed? Thanks much, Amitai pointers are quite confusing...
After editing the code as shown below the warning disappears , but will the result be as expected? ("eXX" to "errXX")
void Serial_Output_Full_Reset(char *outr_message) { char *UART1_error_format = "err"; //prepare the message set_message_in_transbuffer(String_Message, outr_message, NULL, NULL, 0, 0); #if __RS485__ if (&outr_message[1] == 'e') // converting 'eXX' to 'errXX' { strncpy(UART1_error_format, outr_message + 2, 2); UART1_error_format[5] = '\0'; random_write(0 ,0 , nooutr, *UART1_error_format, 2); } #endif }
.
Your code is broken.
char *UART1_error_format = "err";
So the pointer now points to a buffer just big enough - and already initialized with - the string "err\0".
You can't concatenate with any more data.
strncpy(UART1_error_format, outr_message + 2, 2);
This will not place any error number after the "err", since "UART1_error_format" points to the start of the string "err". UART1_error_format+3 will point to the first position after "err". But once more - you then need to make sure that UART1_error_format points to a buffer that is large enough to fit the error code and a terminating zero.
There might be more errors too, but at least the two above has to be fixed.
if (&outr_message[1] == 'e') // NO, WHY THE AMPERSAND?
if (outr_message[0] == 'e')
Thanks to both of you. I've debugged the code and verified that the content of 'UART1_error_format' results correctly. The warning still appears, but why? If there's a problem shouldn't it be at the strncpy function? The unsigned char 'UART1_error_format' doesn't change to a pointer from the copying operation, so the 'random_write' function receives an unsigned char as needed. Attached is the code part for debugging - no other files compiled besides startup.a51
#include "c8051F060.h" #include <string.h> #include <stdlib.h> void random_write(float Treal, float Emiss, unsigned char Outr,unsigned char Err, unsigned char Scale); void main() { char *outr_message = "e99"; unsigned char UART1_error_format[6] = "err"; int i; if (outr_message[0] == 'e') // converting 'eXX' to 'errXX' { strncpy(UART1_error_format + 3, outr_message + 1, 2); UART1_error_format[5] = '\0'; random_write(0 ,0 , 0, UART1_error_format, 2); } } void random_write(float Treal, float Emiss, unsigned char Outr,unsigned char Err, unsigned char Scale) { Treal = Emiss; Outr = 1; Scale = 1; Err = 1; }
I don't use the C51 compiler, and haven't looked at the compiler options available.
But note that the data type "char" might be signed. Or it might be unsigned.
Which means that "unsigned char" and "char" might be different data types. And the strxx() functions takes "char*"- not "unsigned char*".
So sending unsigned char pointer to strncpy() can give a pointer type warning.