【问题标题】:Manipulating dynamic array through functions in C通过 C 中的函数操作动态数组
【发布时间】:2012-12-06 14:47:06
【问题描述】:

我正在学习如何在 C 中使用动态数组。我想做的是创建一个动态数组data,并使用函数test() 将“1”放入第一个条目。

void test(void)
{

data[0] = 1;

}


int main(void)
{

int *data = malloc(4 * sizeof *data);

test();

return 0;
}

这在 Visual Studio 2010 中编译,但程序在运行时崩溃。而不是使用test(),而是使用data[0] = 1

我的(新手)猜测是我需要将一个指向 data 的指针传递给函数 test()。这个要怎么写?

尝试

void test(int *data)
{

data[0] = 1;

}

然后,在main 中使用test(data) 而不仅仅是test()

编辑

尝试有效。但是,这是一种“正确”的做法吗?

【问题讨论】:

  • 你的尝试成功了吗?如果不是,你得到了什么错误,你从中解释了什么?
  • 据我所知,你的第一次尝试不应该编译,因为data 没有在test 函数中定义。您的第二次尝试看起来应该成功了,结果如何?
  • @AshRj - 尝试成功了!这是“正确”的做法吗?

标签: c arrays


【解决方案1】:

当您在 C 中使用局部变量(动态或静态,数组或非数组)时,您需要将它传递给将要使用它的函数。这就是你的初始代码有什么问题,test()data 一无所知。

当你声明一个数组时,(动态或静态)你可以用同样的方式把它传递给函数。下面的代码毫无意义,但它说明了使用动态数组与静态数组并没有什么不同。

void assign_function(int arr[], int len_of_arr, int *arr2, int len_of_arr2);
void print_function(int *arr, int len_of_arr, int arr2[], int len_of_arr2);

int main()
{
    int data[2] = {0}; // static array of 2 ints
    int *data2 = malloc(3 * sizeof(int)); // dynamic array of 3 ints

    assign_function(data, 2, data2, 3);
    print_function(data2, 3, data, 2);

    free(data2); // One difference is you have to free the memory when you're done

    return 0;
}

所以我们可以通过array[] 或作为指针传递动态或静态数组,但我们还需要传递int,以便我们知道数组有多大。

void assign_function(int arr[], int len_of_arr, int *arr2, int len_of_arr2)
{
    int count;
    for(count = 0; count < len_of_arr; count++) //This is the static array
        arr[count] = count;

    for(count = 0; count < len_of_arr2; count++) //This is the dynamic array
        arr2[count] = count;
}

然后只是为了好玩,我在此处反转 arrarr2 传入的数组,以及它们的访问方式:

void print_function(int *arr, int len_of_arr, int arr2[], int len_of_arr2)
{
    int count;
    for(count = 0; count < len_of_arr; count++)   //This is the dynamic array now
        printf("arr[%d] = %d\n", count, *(arr+count));

    for(count = 0; count < len_of_arr2; count++)  //And this is the static array
        printf("arr2[%d] = %d\n", count, *(arr2+count));
}

要点是,通过[] 或作为指针传递,并通过[] 或延迟指针访问取决于您,两者都很好,都可以工作。我尽量避免使用指针,因为它们往往难以阅读并且在编写时更容易出错。

【讨论】:

  • 感谢迈克的冗长解释。 +1
【解决方案2】:

您可以通过两种方式动态传递数组:

  • 使用简单的指针,然后使用指针算法进行操作
void test (int * data, int i)
{
    *(data + i) = 1; //This sets data[i] = 1
}
  • 或者这样:
void test(int data[], int i)
{
    data[i] = 1; //This is the more familiar notation
}

这些方法中的任何一种都是解决此问题的“正确”方法。

【讨论】:

  • 我的尝试成功了。这实际上没有。感谢 cmets 的帮助。你想修改你的答案以便我接受吗?另外,这是一种“正确”的做法吗?
  • @Legendre,是的,这是正确的方法。
  • @Legendre 好吧,您只需要从您的主程序中也使用test(data); 调用测试函数...哦,好的做法,不要忘记@987654324 @你分配的内存。由于在这种情况下您的程序随后退出,操作系统会处理它,但最好始终释放您分配的资源。
  • @Jite - 感谢您提供的建议。 +1
  • 如果您暗示int i 是数组中元素的数量,那么您只是使用data[i] 访问了它之外的内存。如果您暗示 int i 只是一些静态数字,例如 0 来访问数组 data.. 那么这是没有意义的,您应该对其进行硬编码。
【解决方案3】:

测试中的变量“数据”是本地范围的。它与主要的“数据”不同。您应该通过 test() 的参数传递一个指向“数据”的指针。

【讨论】:

    猜你喜欢
    • 2012-12-16
    • 2018-04-17
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2020-07-25
    • 2020-08-09
    相关资源
    最近更新 更多