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

天大地大妈咪最大 提交于 2019-12-06 05:08:59

The & operator simply returns a pointer to its operand. If its operand was an int the resulting type will be int*. If its operand was an int* the resulting type will be int**. For example, this program:

#include <stdio.h>

struct test {
  int a;
  int b;
  int d;
};

int main ( ) { 
    struct test foo = { 0, 0, 0 } ; 
    printf( "%lu\n", (unsigned long)(&foo + 2) - (unsigned long)&foo );
}

Outputs 24 on my machine because the sizeof(struct test) is 12, and the type of &foo is known to be struct test *, so &foo + 2 calculates an offset of two struct tests.

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.

The variable i1 has type int, so the expression &i1 takes the address of an int, yielding an expression of type int *. This expression is then assigned to p1, which is also of type int *.

So now p1 points to the memory location of i1, so the expression *p1 will be 5.

No type awareness in C. C does not check for type safety. Even if you assign some improper address (such as &char to int *), it would only give warning that you are wrong. It won't cause any error.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!