我通常只会使用指针,但在这种情况下我有点卡住了。因为数组的元素以“随机”顺序传递,我似乎无法弄清楚如何设置w
(从方法更新)回到in[0]
(从方法传递)
int main(){
unsigned int array[4] = {
0x10101010,
0x10101010,
0x10101010,
0x10101010};
pass(array);
}
void pass(unsigned int *a){
update(a[0],a[1],a[4],a[3]);
printf("%lu\t %lu\t %lu\t %lu\n",a[0],a[1],a[2],a[3]); //how would I get the updated value here
update(a[1],a[0],a[3],a[4]);
printf("%lu\t %lu\t %lu\t %lu\n",a[0],a[1],a[2],a[3]); //and here
}
/*update method could be of any type like int update(...) or whatever else*/
void update(w,x,y,z){
w = z+2;
x = x+1;
y = y+2;
z = w+1;
}
任何建议或帮助将不胜感激。
PS上面的代码是我只是为了示例目的而编写的。
编辑 * printf 应该使用 %u 而不是 long unsigned (%lu)。更新时没有类型说明符,因为 K&R C(很旧)。
您需要将指针传递给您的update
函数并在该函数内取消对它们的引用:
void update(unsigned int* w, unsigned int* x, unsigned int* y, unsigned int* z){
*w = *z+2;
*x = *x+1;
*y = *y+2;
*z = *w+1;
}
然后update
使用数组元素的地址调用:
update(&a[0], &a[1], &a[4], &a[3]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句