简体   繁体   English

如何在 C 中修改 function 中的数组

[英]How to modify an array inside a function in C

Let's consider the following example:让我们考虑以下示例:

#include <stdio.h>

void change_byte(int *byte);

int main()
{
    int byte = 0;
    change_byte(&byte);
    printf("Byte : %d \r\n", byte);
}

void change_byte(int *byte)
{
    *byte= 5;
}

I am simply changing the value of an integer inside a function by passing the integer as a pointer to the function. I am simply changing the value of an integer inside a function by passing the integer as a pointer to the function. It yields:它产生:

Byte : 5

Everything's fine.一切安好。


I want to generalize the function to modify an array of integer instead of an integer.我想概括 function 来修改 integer 的数组,而不是 integer。 Here is the code:这是代码:

#include <stdio.h>
#define SIZE_ARRAY 10

void change_array(int *array, int size);

int main()
{
    int array[SIZE_ARRAY] = {0};
    change_array(array, SIZE_ARRAY);
    
    printf("Array : ");
    for(int i = 0 ; i < SIZE_ARRAY ; i++)
    {
        printf("%d ", array[i]);
    }
}

void change_array(int *array, int size)
{
    for(int i = 0 ; i < size ; i++)
    {
        array[i] = 5;
    }
}

It yields:它产生:

Array : 5 5 5 5 5 5 5 5 5 5 

I like it because it does not make use of dynamic allocation, but I have trouble understand how it works.我喜欢它,因为它不使用动态分配,但我很难理解它是如何工作的。 From what I understand, array gets converted into a pointer when entering the function change_array.据我了解,当输入 function change_array 时,数组会转换为指针。 But when I was changing the value of byte in the previous example, I was doing *byte = 5. Here, I am doing array[i] = 5 and not *array[i] = 5.但是当我在前面的示例中更改 byte 的值时,我正在执行 *byte = 5。这里,我正在执行 array[i] = 5 而不是 *array[i] = 5。


Finally, I want to change the previous example to modify array based on a global array:最后,我想将前面的示例更改为基于全局数组修改数组:

#include <stdio.h>
#define SIZE_ARRAY 10

int global_array[10] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5};

void change_array(int *array, int size);

int main()
{
    int array[SIZE_ARRAY] = {0};
    change_array(array, SIZE_ARRAY);
    
    printf("Array : ");
    for(int i = 0 ; i < SIZE_ARRAY ; i++)
    {
        printf("%d ", array[i]);
    }
}

void change_array(int *array, int size)
{
    array = global_array;
}

It yields:它产生:

Array : 0 0 0 0 0 0 0 0 0 0   

Why is it so?为什么会这样? What to I need to change to make it work?我需要更改什么才能使其正常工作?

Thanks.谢谢。

For a better understanding (and exercise), edit your function为了更好地理解(和练习),请编辑您的 function

void change_array(int *array, int size)
{
    array = global_array;
}

to

void change_array(int *array, int size)
{
    array = global_array;
    for (int i=0; i < size; ++i)
        printf("%d ", array[i]);
}

Ask yourself, what the output 'means'.问问自己,output 的“含义”是什么。

Consider your first program.考虑您的第一个程序。

#include <stdio.h>

void change_byte(int *byte);

int main()
{
    int byte = 0;
    change_byte(&byte);
    printf("Byte : %d \r\n", byte);
}

void change_byte(int *byte)
{
    *byte= 5;
}

In this program the object byte is passed to the function change_byte by reference through a pointer to it在这个程序中,object byte通过指向它的指针引用传递给 function change_byte

change_byte(&byte);

In C passing by reference means passing an object indirectly through a pointer tp it.在 C 中,通过引用传递意味着通过指针 tp 间接传递 object。

So dereferencing the pointer byte declared as a function parameter所以取消引用声明为 function 参数的指针byte

void change_byte(int *byte);

you get a direct access to the pointed object byte of the type int defined in main.您可以直接访问 main 中定义的int类型的指向 object byte

Now let's consider your second program现在让我们考虑您的第二个程序

#include <stdio.h>
#define SIZE_ARRAY 10

void change_array(int *array, int size);

int main()
{
    int array[SIZE_ARRAY] = {0};
    change_array(array, SIZE_ARRAY);
    
    printf("Array : ");
    for(int i = 0 ; i < SIZE_ARRAY ; i++)
    {
        printf("%d ", array[i]);
    }
}

void change_array(int *array, int size)
{
    for(int i = 0 ; i < size ; i++)
    {
        array[i] = 5;
    }
}

In main you declared an integer array您主要声明了一个 integer 数组

int array[SIZE_ARRAY] = {0};

Array designators used in expressions with rare exceptions are converted to pointers to their first elements.表达式中使用的数组指示符(很少有例外)被转换为指向其第一个元素的指针。

Thus this call因此这个电话

change_array(array, SIZE_ARRAY);

is equivalent to相当于

change_array( &array[0], SIZE_ARRAY);

So dereferencing the pointer within the function you can change the first element of the array defined in main.因此,取消引用 function 中的指针,您可以更改 main.xml 中定义的数组的第一个元素。

But array elements are stored in a continuous extent of memory.但是数组元素存储在 memory 的连续范围中。 So using the pointer arithmetic and having a pointer to the first element of an array you can access all elements of the array.因此,使用指针算法并拥有指向数组第一个元素的指针,您可以访问数组的所有元素。

In fact all elements of the array array are passed to the function change_array by reference through a pointer to the first element of the array.实际上,数组array的所有元素都通过指向数组第一个元素的指针通过引用传递给 function change_array

For example the for loop within the function you could rewrite like例如 function 中的 for 循环,您可以像这样重写

    for(int i = 0 ; i < size ; i++)
    {
        *( array + i ) = 5;
    }

Now let's consider your third program.现在让我们考虑您的第三个程序。

#include <stdio.h>
#define SIZE_ARRAY 10

int global_array[10] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5};

void change_array(int *array, int size);

int main()
{
    int array[SIZE_ARRAY] = {0};
    change_array(array, SIZE_ARRAY);
    
    printf("Array : ");
    for(int i = 0 ; i < SIZE_ARRAY ; i++)
    {
        printf("%d ", array[i]);
    }
}

void change_array(int *array, int size)
{
    array = global_array;
}

As it was pointed out already the array array passed to the function change_array is converted to a pointer to its first element.正如已经指出的那样,传递给 function change_array的数组array被转换为指向其第一个元素的指针。

You may imagine the function call and its definition the following way (I will rename the first function parameter that to avoid name ambiguity).您可以想象 function 调用及其定义如下(我将重命名第一个 function 参数以避免名称歧义)。

change_array(array, SIZE_ARRAY);

//...

void change_array( /* int *parm_array, int size */)
{
    int * parm_array = array;
    int size = SIZE_ARRAY;

    parm_array = global_array;
}

That is function parameters are its local variables.即function参数是它的局部变量。 The parameter parm_array is alive until the function stops its execution.参数parm_array一直有效,直到 function 停止执行。

Thus this statement因此这个说法

    parm_array = global_array;

assign the pointer to the first element of the global array global_array to the local variable parm_array of the function.将指向全局数组global_array的第一个元素的指针分配给 function 的局部变量parm_array This assignment does not touch in any way the array array defined in main.此分配不会以任何方式触及 main 中定义的数组array It only changes the local variable parm_array declared in the function change_array .它只更改在 function change_array parm_array

To achieve the expected by you result you could define in main a pointer that is initialized by the array array .为了达到您预期的结果,您可以在 main 中定义一个由数组array初始化的指针。 And in the function change_array you could reassigned the pointer with the array global_array passing it to the function by reference the same way as you passed the object byte in your first program.在 function change_array中,您可以重新分配指针,数组global_array通过引用将其传递给 function,方法与您在第一个程序中传递 ZA8CFDE6331BD59EB2AC96F8911C4B66Z byte的方式相同。

Here is a demonstrative program.这是一个演示程序。

#include <stdio.h>
#define SIZE_ARRAY 10

int global_array[10] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5};

void change_array( int **array_ptr );

int main()
{
    int array[SIZE_ARRAY] = {0};
    int *array_ptr = array;
    
    change_array( &array_ptr );
    
    printf("Array : ");
    for(int i = 0 ; i < SIZE_ARRAY ; i++)
    {
        printf("%d ", array_ptr[i]);
    }
}

void change_array( int **array_ptr )
{
    *array_ptr = global_array;
}

The program output is'程序 output 是'

Array : 5 5 5 5 5 5 5 5 5 5 

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

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