Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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*?

like image 297
Pavan Manjunath Avatar asked Mar 22 '12 15:03

Pavan Manjunath


People also ask

What is the difference between signed char and unsigned char?

An unsigned type can only represent postive values (and zero) where as a signed type can represent both positive and negative values (and zero). In the case of a 8-bit char this means that an unsigned char variable can hold a value in the range 0 to 255 while a signed char has the range -128 to 127.

Should I use signed or unsigned char?

Unsigned char must be used for accessing memory as a block of bytes or for small unsigned integers. Signed char must be used for small signed integers and simple char must be used only for ASCII characters and strings.

Why do you need signed and unsigned char?

Signed char and unsigned char both are used to store single character. The variable stores the ASCII value of the characters. For an example if 'A' is stored, actually it will hold 65. For signed char we need not to write the signed keyword.

What is the purpose of unsigned char?

Unsigned char data type in C++ is used to store 8-bit characters. A maximum value that can be stored in an unsigned char data type is typically 255, around 28 – 1(but is compiler dependent).


1 Answers

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;
}
like image 135
perreal Avatar answered Oct 04 '22 00:10

perreal