cpointersintegershort

Assigning a short to int * fails


I understand that I can reassign a variable to a bigger type if it fits, ad its ok to do it. For example:

short s = 2;
int i = s;
long l = i;
long long ll = l;

When I try to do it with pointers it fails and I don't understand why. I have integers that I pass as arguments to functions expecting a pointer to a long long. And it hasn't failed, yet..

The other day I was going from short to int, and something weird happens, I hope someone can I explain it to me. This would be the minimal code to reproduce.

short s = 2;
int* ptr_i = &s; // here ptr_i  is the pointer to s, ok , but *ptr_i   is definitely not 2

Solution

  • When I try to do it with pointers it fails and I don't understand why.

    A major purpose of the type system in C is to reduce programming mistakes. A default conversion may be disallowed or diagnosed because it is symptomatic of a mistake, not because the value cannot be converted.

    In int *ptr_i = &s;, &s is the address of a short, typically a 16-bit integer. If ptr_i is set to point to the same memory and *ptr_i is used, it attempts to refer to an int at that address, typically a 32-bit integer. This is generally an error; loading a 32-bit integer from a place where there is a 16-bit integer, and we do not know what is beyond it, is not usually a desired operation. The C standard does not define the behavior when this is attempted.

    In fact, there are multiple things that can go wrong with this:

    I have integers that I pass as arguments to functions expecting a pointer to a long long.

    C does allow default conversions of integers to integers that are the same width or wider, because these are not usually mistakes.