Author Topic: Help with simple pointer to char array problem coding with C99 please!  (Read 937 times)

0 Members and 1 Guest are viewing this topic.

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
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[20];
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[20];
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?


 

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Re: Help with simple pointer to char array problem coding with C99 please!
« Reply #1 on: November 20, 2018, 03:38:54 pm »
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.
« Last Edit: November 20, 2018, 03:57:02 pm by SuzyC »
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8186
  • Country: fi
Re: Help with simple pointer to char array problem coding with C99 please!
« Reply #2 on: November 20, 2018, 04:09:42 pm »
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.
« Last Edit: November 20, 2018, 04:13:32 pm by Siwastaja »
 

Offline up8051

  • Frequent Contributor
  • **
  • Posts: 288
  • Country: pl
Re: Help with simple pointer to char array problem coding with C99 please!
« Reply #3 on: November 20, 2018, 04:11:56 pm »
Try:

strcpy((char*)bp, "Hellow");

up8051 aka JarekC.DIY
 
The following users thanked this post: SuzyC

Offline tri-be

  • Supporter
  • ****
  • Posts: 9
Re: Help with simple pointer to char array problem coding with C99 please!
« Reply #4 on: November 20, 2018, 04:14:46 pm »
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.
 
The following users thanked this post: SuzyC


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf