C: Does the address operator (&) produce a pointer (address + type) or just an address?

后端 未结 4 1356
感动是毒
感动是毒 2021-01-14 23:30

Most of what I\'ve read about the address operator, &, says it\'s used to get just that - an address. I recently heard it described differently, though, as

4条回答
  •  庸人自扰
    2021-01-14 23:56

    While you're right that the type of &i1 is int*, I think you're assigning too much weight to that idea. I wouldn't describe anything in C as "type-awareness" and there's definitely no "remembering that the data in that location is to be interpreted as type int". C types exist only at the source level and not after compilation.

    This may be clearer in an example with union:

    union something {
        int a;
        char b;
    };
    

    now if you have a variable x of type something, you can have &x.a of type int* and &x.b of type char*, but they're pointing at the same address.

提交回复
热议问题