result: 65Code:
char *tt = "A";
printf("%d\n",*(unsigned char *) tt);
printf("%d\n",*tt);
65
why are they same ?
what are *tt different from **tt?
Printable View
result: 65Code:
char *tt = "A";
printf("%d\n",*(unsigned char *) tt);
printf("%d\n",*tt);
65
why are they same ?
what are *tt different from **tt?
Why would they not be the same?Quote:
Originally Posted by zcrself
In this case **tt does not make sense, but if tt is a pointer to a pointer, then *tt is a pointer and **tt is what that pointer points to.Quote:
Originally Posted by zcrself
By the way, since "A" is a string literal and string literals must not be modified, you should have written:
Code:const char *tt = "A";
I don't see **tt in the code you posted.
Casting the pointer doesn't change where it points. You've got *tt in the first one, and *tt in the second one, and surprisingly enough those are the same thing.
Well, tt is a char*, so *tt is a char (depending on the platform that could be a signed or unsigned value). Then *( unsigned char* )tt is (possibly) just casting a char to an unsigned char. Since they're the same size, there's no chance of the value getting truncated or extended in the process (in contrast to a char/size_t conversion, for instance). In other words, assuming an 8-bit signed char containing the value -1, the bit pattern would be:
11111111
Casting it to an unsigned char reinterprets it as 255, which has a bit pattern of:
11111111
Same data, different interpretation.
Ah, but remember that we are talking about pointers here.
Presumably, all pointers have the same size (I think we can pretty much rely on that), so it doesn't do anything bad changing the type of the pointer.
We must remember that in memory, all data are just bits: there is no "type". Type is something the compiler has invented to make our lives easier. Different types represent how the compiler stores and reads that raw information and presents it to us.
> Presumably, all pointers have the same size (I think we can pretty much rely on that)
nope, can't rely on that.
Oh? Can you point out where we cannot rely on this?
But rely, I mean that we can probably be sure it works in most cases*.
*) Meaning that it may not be written in the standard, but 99% of the platforms have the same size on all pointer types (except perhaps void*).