[英]Array Assignments in C Using Pointer Arithmetic
How can I change the value in an array when I access a particular element using pointer arithmetic? 使用指针算术访问特定元素时,如何更改数组中的值?
#include <stdio.h>
int main() {
int a[3] = {1, 1, 1}, b[3] = {2, 2, 2};
a++ = b++; // How can I get this to work so a[1] = b[1]?
return 0;
}
Arrays are not pointers. 数组不是指针。 Repeat this three times; 重复三遍; arrays are not pointers . 数组不是指针 。
You cannot increment an array, it is not an assignable value (ie, you cannot mutate it). 您不能递增数组,它不是可分配的值(即,您不能对其进行突变)。 You can of course index into it to get a value back: 您当然可以对其进行索引以获取返回的值:
a[1] = b[1];
Secondly, your current code is attempting to increment and then assign a new value to the array itself , when you meant to assign to an element of the array. 其次,当您要分配给数组的元素时,当前代码将尝试递增,然后为数组本身分配一个新值。 Arrays degrade to pointers when required, so this works too: 数组在需要时降级为指针,因此也可以使用:
int *a_ptr = a;
int *b_ptr = b;
*++a_ptr = *++b_ptr;
// or, better...
a_ptr[1] = b_ptr[1];
Which is what you meant to do. 那是你的意思。 I prefer version 1 and, more often than not, use indexing with pointers as well because it is often easier to read. 我更喜欢版本1,并且更经常地也使用带有指针的索引,因为它通常更易于阅读。
How can I get this to work so a[1] = b[1]? 我如何才能使它工作,以便a [1] = b [1]?
Simple: 简单:
a[1]++;
if you just wanted to increment a[1]
(1) to be what b[1]
happens to be (2), or 如果您只是想将a[1]
(1)递增a[1]
b[1]
恰好是(2),或者
a[1] = b[1];
if you want a[1]
to have the same value as b[1]
regardless of what that value is. 如果您希望a[1]
与b[1]
具有相同的值,而不管该值是多少。
when I access a particular element using pointer arithmetic? 当我使用指针算法访问特定元素时?
In your example, you are not accessing any element, nor are you doing pointer arithmetic because a
and b
are arrays, not pointers. 在您的示例中,您没有访问任何元素,也没有进行指针算术,因为a
和b
是数组,而不是指针。 The formulation of your question is difficult to interpret, both because of that and because 因为这个原因,而且因为
a++ = b++;
1) is completely meaningless 2) would not be legal C even if a
and b
were pointers, because the left side must be an lvalue, but a++
is not 3) is not discernably related to your wish for a[1]
to be the same as b[1]
. 1)完全没有意义2)即使a
和b
是指针,也不会合法C,因为左侧必须是左值,但是a++
不是3)与a[1]
是与b[1]
相同。 Possibly what you want is: 您可能想要的是:
int* ap = a; // get pointer to first element of a
int* bp = b; // get pointer to first element of b
// point ap to second element of a and
// point bp to second element of b and
// copy the value at *bp to *ap
*++ap = *++bp;
That would indeed set a[1]
to b[1]
. 确实会将a[1]
设置为b[1]
。
Your arrays in this case are not actually pointers. 在这种情况下,您的数组实际上不是指针。 They are converted by the compiler when they are accessed as pointers, but I don't believe that you're allowed to do something like a++
. 当它们作为指针访问时,它们会由编译器转换,但是我不认为您可以做类似a++
事情。
If you want to do this with arithmetic, you'll need actual pointers: 如果要使用算术运算,则需要实际的指针:
int *ap = a, *bp = b;
*ap++ = *bp++;
That is like doing a[0] = b[0];
就像做a[0] = b[0];
and then moving each pointer to the next element in their associated array. 然后将每个指针移到关联数组中的下一个元素。
But your question says you want to set a[1] = b[1]
. 但是您的问题是您想设置a[1] = b[1]
。 Well, you could do this: 好吧,你可以这样做:
*++ap = *++bp;
Or you could just use the array indices and make it much more obvious what you're doing. 或者,您可以只使用数组索引并使您所做的事情更加明显。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.