简体   繁体   English

C指针和地址

[英]C pointers and addresses

I always thought that *&p = p = &*p in C. I tried this code: 我一直以为C中的*&p = p =&* p。我尝试了以下代码:

 #include <stdio.h>
 #include <stdlib.h>

 char a[] = "programming";
 char *ap = &a[4];  

int main(void)
{

 printf("%x %x %x\n", ap, &*(ap), *&(ap));   /* line 13 */
 printf("%x %x %x\n\n", ap+1, &*(ap+1), *&(ap+1));   /* line 14 */
}

The first printf line (line 13) gives me the addresses: 第一行printf行(第13行)为我提供了地址:

40b0a8 40b0a8 40b0a8 40b0a8 40b0a8 40b0a8

which are the same as expected. 与预期相同。 But when I added the second printf line, Borland complains: 但是,当我添加第二条printf行时,Borland抱怨:

"first.c": E2027 Must take address of a memory location in function main at line 14 “ first.c”:E2027必须在第14行的函数main中获取一个存储位置的地址

I was expecting to get: 我期待得到:

40b0a9 40b0a9 40b0a9. 40b0a9 40b0a9 40b0a9。

It seems that the expression *&(ap+1) on line 14 is the culprit here. 第14行上的表达式*&(ap + 1)似乎是罪魁祸首。 I thought all three pointer expressions on line 14 are equivalent. 我认为第14行上的所有三个指针表达式都是等效的。 Why am I thinking wrong? 我为什么想错了?

A second related question: The line 第二个相关问题:线

char *ap = a;

points to the first element of array a. 指向数组a的第一个元素。 I used 我用了

char *ap = &a[4];  

to point to the 5th element of array a. 指向数组a的第五个元素。

Is the expression 是表达

char *ap = a;

same as the expression 与表达式相同

char *ap = &a[0];

Is the last expression only more verbose than the previous one? 最后一个表达式是否仅比前一个更冗长?

Thanks a lot... 非常感谢...

You can only take the address of an lvalue , ie an expression that refers to an object. 您只能使用左值的地址,即引用对象的表达式。 ap + 1 is an address calculation. ap + 1是地址计算。 It has a value but it's a temporary object so isn't an lvalue and you can't take its address. 它有一个值,但它是一个临时对象,因此不是左值 ,您不能使用其地址。

In answer to your second question, in most contexts in expressions an array decays to a pointer to it's first element so yes, char *ap = a; 为了回答您的第二个问题,在大多数情况下,在表达式中,数组会衰减为指向其第一个元素的指针,所以,是, char *ap = a; and char *ap = &a[0]; char *ap = &a[0]; are equivalent. 是等效的。

When you use the C reference operator, it has to point to a valid lvalue, not an arbitrary expression. 使用C引用运算符时,它必须指向有效的左值,而不是任意表达式。 Thus, &(ap+1) isn't valid because the value ap+1 is simply an expression, not a location. 因此, &(ap+1)无效,因为值ap+1只是一个表达式,而不是位置。 You can't say ap+1 = foo(); 你不能说ap+1 = foo();

And yes, a is the same as &a[0] here. 是的,a与&a [0]相同。 Note that *(a+b) is 100% equivalent to a[b] (see the top answer to Strangest language feature for an unusual example of this equivalence). 请注意,*(a + b)等于a [b] 100%(有关此等价的不寻常示例,请参见最奇怪的语言功能的最高答案)。 When getting a pointer to a member of an array, you can use &array[i] or array + i. 获取指向数组成员的指针时,可以使用&array [i]或array + i。 Example: 例:

struct foo array[5];
struct foo *item_3 = &array[3];
struct foo *also_item_3 = array + 3;

In this case, whether to use array+i or &array[i] is a matter of style. 在这种情况下,使用array+i还是&array[i]是一个风格问题。 &array[i] is arguably a better choice, as it is clearer that an array item is being gotten. &array[i]可以说是一个更好的选择,因为很明显要获取数组项。 Moreover, &vec[i] works with C++'s vectors, whereas vec+i does not. 而且,&vec [i]可与C ++的向量一起使用,而vec + i则不能。

If you believe one of those statements is the culprit specifically, I would break that line into three separate lines and see where the compiler complains at you. 如果您认为这些语句之一是罪魁祸首,那么我将把这一行分成三行,看看编译器在哪里抱怨您。 I bear the same suspicion, but to confirm it I would do just as I just told you to do. 我也有同样的怀疑,但是要确认,我会按照我刚才告诉你的那样做。

I believe Charles is correct about your main question, and you are correct about the second question: char *ap = a; 我相信Charles对您的主要问题是正确的,而您对第二个问题是正确的char *ap = a; is equivalent to char *ap = &a[0]; 等同于char *ap = &a[0]; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM