通过数组,指针在C诠释 [英] Passing arrays, pointers to int in C

查看:101
本文介绍了通过数组,指针在C诠释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经过多年的Matlab的工作是新的C数值分析。我有一个构造一个数组的函数,我需要得到它回到主()。我曾与数组指针与一般的麻烦,并通过了蹊跷想出如何做到这一点。但摆弄给我留下基于以下code的概念问题。

I'm new to C for numerical analysis after many years of working with Matlab. I have a function that constructs an array and I need to get it back to main(). I had the usual trouble with arrays vs. pointers, and by fiddling figured out how to do this. But the fiddling left me with a conceptual question based on the following code.

#include <stdio.h>
void array_set(int y_out[2][2]);
void int_set_wrong(int  y);
void int_set_right(int *y);


int main (int argc, const char * argv[]) {

    int y_array[2][2]={{0,0},{0,0}};
    int y_int_1 = 0;
    int y_int_2 = 0;

    array_set(y_array);
    int_set_wrong(  y_int_1 );
    int_set_right( &y_int_2 );

    printf("\nValue array: %d \n",y_array[0][0]);
    printf("Value int wrong: %d \n",y_int_1);
    printf("Value int right: %d \n",y_int_2);

    return 0;
}

void array_set(int y_out[2][2]){

    y_out[0][0] = 10;
    y_out[1][0] = 20;
    y_out[0][1] = 1;
    y_out[1][1] = 2;
}

void int_set_wrong(int y){

    y = 10;

}

void int_set_right(int * y){

    *y = 10;

}

以上回报的代码片段:
数值数组:10
VALUE INT错误:0
VALUE INT右:10

The snippet above returns: Value array: 10 Value int wrong: 0 Value int right: 10

我的问题是在几个地方,

My question is in a few parts,

第一,为什么功能array_set工作?我希望它以同样的方式,int_set_wrong没有失败。

first, why does the function 'array_set' work? I would expect it to fail in the same way that 'int_set_wrong' did.

如何是整数和int不同的方式在传递处理数组?

How are ints and int arrays treated differently in passing?

此外,在int_set_wrong的情况下,有没有y的本地版本?

Furthermore, in the case of 'int_set_wrong', is there a local version of y?

如果是这样,为什么会出现不y_out在设置阵列的情况下,本地版本?

If so, why is there not a local version of y_out in case of setting the array?

感谢您的帮助。顺便说一句,如果有什么事情,会导致问题,我实现array_set,请附和

Thanks for the help. As an aside, if there is anything that will cause problems with my implementation of array_set, please chime in.

- 安德鲁

推荐答案

int_set_right()您传递的变量 y_int_2 (你和宣布,以的main()),通过运营商&安培;

In int_set_right() you passed the address of the variable y_int_2 (which you declared and defined in main()) through the operator &.

int_set_right( &y_int_2 );

由于该功能能够获得实际的变量 y_int_2 通过指针(设置的值y_int_2 ),您分配 y_int_2 &放 y_int_2 地址>值 10 当你分配 * Y = 10

Since the function had access to the actual variable y_int_2 through the pointer y (set to the value of the address of y_int_2 from &y_int_2), you assigned y_int_2 the value 10 when you assigned *y = 10.

您可以阅读 * Y = 10 为:

,其变量的值
  地址存储在现在设置为
  10

the value of the variable whose address is stored in y is now set to 10

的为 y_int_1 ,你仅仅通过的的。因此,一个临时变量在创建时你叫 int_set_wrong()这与在值初始化的 y_int_1 。因此,所有你所做的就是更改临时的价值的本地的变量(本地 int_set_wrong())。

But for y_int_1, you merely passed the value. So a temporary variable was created when you called int_set_wrong() which was initialized with the value of y_int_1. So all you did was change the value of the temporary of the local variable (local to int_set_wrong()).

这就是为什么 y_int_1 宣布的main()不受 int_set_wrong ()

array_set 的作品,因为你通过了 y_array 的地址通过 y_out功能变量(这是在像指针 int_set_right )。

array_set works because you passed the address of y_array to the function through the y_out variable (which is a pointer like y in int_set_right).

这篇关于通过数组,指针在C诠释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆