Help with simple pointer to char array problem coding with C99 please! - Page 1
Forum section: electronics / general

author: SuzyC
author_rank: Frequent Contributor
timestamp: November 20, 2018, 03:27:15 pm
content: Working with Hi-Tech C with a PIC mcu, I can successfully code the following lines and tested, everything works fine:static unsigned char Buf;static unsigned char * bp=Buf;//and in main() there is no problem with:strcpy(bp, "Hellow");//Here's the problem: Writing PSOC code with Cypress C99 I can again code this with no warnings or errors:static unsigned char Buf;static unsigned char * bp=Buf;//But when I then code:strcpy(bp, "Hellow");//I get the following warning: "passing 'unsigned char *' to parameter of type "char*" converts to integer types of different signs"//So please help..can anyone advise if I need to and how to fix this?

author: SuzyC
author_rank: Frequent Contributor
timestamp: November 20, 2018, 03:38:54 pm
content: I tried to delete the duplicate post I made in error because my PC seemed to lock up for a long time. Unfortunately, I can't.I still don't quite understand how pointers can be signed, don't pointers reference a positive memory address?And isn't declaring a var or pointer "char" the same as "unsigned char" by default?But I don't quite understand why pointers would would be converted to integers with different sign???Is it because C99 is C+ instead of just C ?These ideas are important to me because I need sometimes to embed parsing and delimiting characters in char digit display buffer arrays that have no ASCII equiv and a numerical value >127.

author: Siwastaja
author_rank: Super Contributor
timestamp: November 20, 2018, 04:09:42 pm
content: The pointers don't have signs - the pointers themselves are OK.They are pointing to different kinds of data: unsigned char, or signed char. This is what you get the warning from.It's a bit surprising, but the char, used to represent the ASCII characters, come in both unsigned and signed forms. More confusingly, they are sometimes used interchangeably - the wrap around magically "fixes" the issue.Having separate signed and unsigned char makes a lot of sense when you use "char" as a general-purpose 8-bit storage; not to represent a character. In this case, I strongly suggest using stdint.h types. For C string, just use char[] and char*.AFAIK, "char" is implementation defined, so internally, it can be either "unsigned char" or "signed char". This being said, the standard C libraries are built so that just using "char" alone should always work.

author: up8051
author_rank: Frequent Contributor
timestamp: November 20, 2018, 04:11:56 pm
content: Try:strcpy((char*)bp, "Hellow");up8051 aka JarekC.DIY

author: tri-be
author_rank: Unknown
timestamp: November 20, 2018, 04:14:46 pm
content: The type of a pointer is of what it points to, rather than the pointer itself.char usually represents 8-bit data which can be signed or unsigned depending on other keywords.A compiler can treat char (without other keywords) as either signed or unsigned by default, which somebody chose to suit the environment.The compiler for the first example must have char as unsigned by default, the second compiler has char as signed by default. It should be possible to change the default with compiler parameters, but you want to avoid doing that unless you want to recompile system libraries and have a non-standard environment.Assuming you have a reason for your data to be unsigned char, the simplest way is to cast the type of your pointer to match the strcpy parameter type.strcpy( (char *) bp, "Hellow");Note that casting pointers is a normal thing to do but it is a blunt weapon that you should take care using.