简体   繁体   中英

Whats the working difference between a signed char pointer and an unsigned one?

I can understand the difference between a signed char and an unsigned one. But dont the pointers of the corresponding type equivalent in their operation? Cos sizeof(char) and sizeof(unsigned char) is always the same ( Or are there any counter examples? )

For pointers, only the size of the data type should matter. Are there any pitfalls if I use char * and unsigned char * interchangeably?

I din't find these posts as useful :(

Difference between unsigned char and char pointers

Why short* instead of char* for string? Difference between char* and unsigned char*?

Here is a counter example based on the difference in ranges of types pointed by each pointer type:

#include <stdio.h>
int main() {
  unsigned char *uc;
  char *c;
  unsigned char v = 128;
  uc = &v;
  c  = &v;
  if (*uc != *c)
    printf("there is difference\n");
  return 0;
}

The type of the pointers is telling the compiler the type of the thing that the pointer points to - thus enabling the compiler to ensure that you are using that pointer in the correct fashion and trying to ensure that you are not making any errors.

The compiler with its errors and warnings is trying its best to ensure that the code that you develop is correct. Take heed of them - the people that write compilers are trying to help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM