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

喜你入骨 提交于 2020-01-02 10:22:24

问题


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 producing a full-fledged pointer.

Given the following C code,

int i1 = 5;
int *p1;

p = &i1;

my understanding is that p1 references the int 5 by storing the address where i1's data is stored and remembering that the data in that location is to be interpreted as an int (which dictates how many bytes to read and how to interpret the read data).

Does the address operator yield both the address and the "type-awareness", or is the type-awareness just implicit since p1 was declared to be an int pointer?

One of the things that got me wondering about this was seeing code like:

void myFunc(int *ptr);

int i = 5;
myFunc(&i);

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/34702112/c-does-the-address-operator-produce-a-pointer-address-type-or-just-an-a

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