0

I'm trying to understand how pointers work. I created an int value and made a char pointer to point at it.

When printing the content of the address that char pointer points to, I don't get the expected result.

Like if that char pointer is pointing to 256, I was expecting the content of that address to return 0 because (256)10 = (0000000100000000)2. Because a char pointer points to one byte so it'll return the first 8 bits which are zeros.

But it returns -1.

Here's my code

#include <stdio.h>

int main()
{
    int y = 256;
    char *p = (char *)&y;

    // returns Value -1
    printf("Value %d \n", *p);

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Rafael Adel
  • 7,673
  • 25
  • 77
  • 118
  • Perhaps because the anti-aliasing rule is broken which lead to UB? The value in `y` is not known to be used and so it is optimized out? – chux - Reinstate Monica Dec 02 '15 at 18:51
  • That's curious. What machine are you running on? O/S? Compiler? It could be partly an 'endianness' issue, but superficially, you should be getting a zero whether it is big-endian or little-endian. When I test the code on both RHEL 5 and Mac OS X 10.11.1, I get 0 output, as I'd expect. – Jonathan Leffler Dec 02 '15 at 18:52
  • @chux: Aren't `char *` exempt from anti-aliasing? (And I think we both mean 'strict aliasing' — anti-aliasing is related to things like fonts and pixels, isn't it?) – Jonathan Leffler Dec 02 '15 at 18:52
  • @Jonathan Leffler I think you are correct. – chux - Reinstate Monica Dec 02 '15 at 18:53
  • 3
    @chux Any object can be accessed through a character pointer without violating strict-aliasing. It's not UB. – P.P Dec 02 '15 at 18:53
  • 1
    @Rafael Are you sure you got -1 as output for 256, not 255? – P.P Dec 02 '15 at 18:55
  • @Blue Moon Does the anti-aliasing rule exemption to `char *`, `unsigned char *`, etc? – chux - Reinstate Monica Dec 02 '15 at 18:57
  • That's weird `p = &y` caused the problem, but `p = (char *)&y` didn't. I'm curious why though. And also shouldn't `255` returns 255 as well ? – Rafael Adel Dec 02 '15 at 18:57
  • @chux Yes. There's another type to that list: `signed char*`. C standard considers the 3 of them as distinct types. – P.P Dec 02 '15 at 18:58
  • What do you get with `printf("Value %d %d\n", *p, y);`? – chux - Reinstate Monica Dec 02 '15 at 19:00
  • 1
    @chux for y = 256, I get "Value 0 256". But for y = 255, I get "Value -1 255" – Rafael Adel Dec 02 '15 at 19:02
  • 1
    This differs from your original post that had `y=256` and "it returns -1". Now the answer is easy. code is simply printing the least significant byte of `y` as a `signed char`. – chux - Reinstate Monica Dec 02 '15 at 19:04
  • @chux Sorry, but what do you mean by "The original case could be explained by `y` being optimized out as it was not used." – Rafael Adel Dec 02 '15 at 19:08
  • When code explicitly uses `y` as in `printf("Value %d %d\n", *p, y);`, then `y` is used and must have the _value_ of 255 or 256 or whatever. Without using `y`, but only the _address_ of `y` as in `char *p = (char *)&y` it is unclear to me if a compliant compiler must maintain the value of `y`. It that were the case, possible the compiler did not initialize `y` as thought. – chux - Reinstate Monica Dec 02 '15 at 19:12
  • 1
    @chux But why 255 still gives me -1 ? Even though i'm printing `y` – Rafael Adel Dec 02 '15 at 19:16
  • 1
    Getting `Value -1 255` when `y` is 255 makes sense. `p` points to a byte that looks like `11111111` in binary, `*p` is a signed char, and `%d` prints a signed value, so that's why it prints -1. – dbush Dec 02 '15 at 19:30
  • @dbush Ah, I totally forgot about the MSB. Thanks a lot. – Rafael Adel Dec 02 '15 at 19:34

1 Answers1

0

But why 255 still gives me -1 ? Even though i'm printing y

Getting Value -1 255 when y is 255 makes sense. p points to a byte that looks like 11111111 in binary, *p is a signed char, and %d prints a signed value, so that's why it prints -1. – dbush

Armali
  • 18,255
  • 14
  • 57
  • 171