-9

What is an unsigned char pointer and how is it different from a char pointer in C?

Not much information on unsigned char pointer in C , and How to print the data pointed by an unsigned char pointer?

Any explanation on this will be helpful.

  • 2
    The problem is in the line of code you didn't show. Otherwise the message is pretty clear: %d expcects an int and you provide an unsigned int*. This is _undefined behaviour_ (google that) – Jabberwocky Mar 18 '20 at 11:03
  • Just added a sample c code snippet : unsigned char* temp; temp= 133+2; printf("%d",temp); – Vibhuti Sawant Mar 18 '20 at 11:09
  • You probably meant to do `printf("%u",(unsigned int)*temp);` – Lundin Mar 18 '20 at 11:11
  • I think you should read the chapter dealing with pointers in your C text book. `char* temp; temp= 133+2;` is pretty pointless, maybe you meant `char temp; temp= 133+2;`? – Jabberwocky Mar 18 '20 at 11:12
  • @Lundin I don't think so, look at his code snippet in the second comment – Jabberwocky Mar 18 '20 at 11:13
  • @VibhutiSawant remove the star `*` – 0___________ Mar 18 '20 at 11:14
  • @P__J__ probably, but without more information from the OP it's hard to tell. – Jabberwocky Mar 18 '20 at 11:16
  • @VibhutiSawant what is you code supposed to do? What output do you expect. Probably just `unsigned char* temp;` -> `unsigned char temp;`, but we need more information – Jabberwocky Mar 18 '20 at 11:25
  • @Jabberwocky the main aim to ask the question was to know the functionality of unsigned char pointer, and how to get the data its pointing to. I am more interested in knowing the memory representation and the byte of memory temp is referring to. – Vibhuti Sawant Mar 18 '20 at 11:57

3 Answers3

1

Assuming you have something like

unsigned char *p = something;

then

printf( “p = %p\n”, (void *)p );

prints the value of the pointer itself, while

printf( “*p = %hhu\n”, *p );

prints the value of what p points to as a decimal integer (unsigned).

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Not quite sure if this is the correct answer. I suspect the OP doesn't even know what a pointer is, but without more information it's hard to tell – Jabberwocky Mar 18 '20 at 11:18
0

the documentation is here: https://linux.die.net/man/3/printf

unsigned char x = 0x4d;
unsigned char *y = &x;

printf("pointer: %p, %hhu\n", (void *)y, *y);

endianess do not matter as sizeof(unsgined char) id always 1.

If you want to pun types via pointers - it is generally a bad idea.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Not quite sure if this is the correct answer, apparently the OP provided the a pointer instead of value, but without seeing his code it's hard to tell. – Jabberwocky Mar 18 '20 at 11:07
0

How to print the data pointed to by an unsigned char* p?

That depends entirely on the datas format. And what you want actually have and what you need.

Is it just a binary blob of known length?
You probably want a hexdump then. Search for it, while it is common, it is not builtin, and there are many options.

Is it really a null terminated string?
Print it using %s after casting to char*.

Is it just a single unsigned char?
Print it using %hhu after dereferencing.

There is really insufficient information to judge what is needed.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118