简体   繁体   English

C按值传递数组?

[英]c pass array by value?

I have this homework problem: 我有这个作业问题:

Part A 甲部

Write a C program that tests whether or not the following data types are passed by reference or by value, and prints what it discovers out to the terminal: 编写一个C程序,以测试以下数据类型是通过引用还是通过值传递,并将发现的结果打印到终端:

  • int 整型
  • array of ints 整数数组

Hypothetically, if your program discovers that an int is passed by value and an array of ints is passed by value, then it should produce output like: int: pass-by-value array of ints: pass-by-value 假设,如果您的程序发现一个int按值传递,并且一个int数组按值传递,那么它应该产生如下输出:int:按值传递数组ints:按值传递

The int I understand, but what I don't get is: 我了解的诠释,但我没有得到的是:

  1. I thought the only way to pass an array was to pass the address of the first value in the array 我认为传递数组的唯一方法是传递数组中第一个值的地址
  2. does this count as passing by value or passing by reference? 这算作按值传递还是按引用传递? ( Pass an array to a function by value confused me) 按值将数组传递给函数使我感到困惑)
  3. Hints on how I can do this? 有关如何执行此操作的提示? I'm assuming something like passing the reference to another function, manipulating it, and seeing if it changed, but I'm not sure.... 我假设类似将引用传递给另一个函数,对其进行操作并查看其是否发生更改,但是我不确定...。

Edit: might help if someone explained this to me with a concrete example, such as: Say I have an array of length 10 ints stored at memory location 0 (yeah, I know, not real life, but for the sake of the example...). 编辑:如果有人通过一个具体的例子向我解释这可能会有所帮助,例如:假设我在存储位置0处存储了一个长度为10个int的数组(是的,我知道,不是现实生活,但出于示例原因。 ..)。 What would it look like if that array was passed by reference? 如果该数组通过引用传递会是什么样子? What would it look like if it was passed by value? 如果按值传递它会是什么样?

Does this count as passing by value or passing by reference? 这算作按值传递还是按引用传递?

When you say "passing an array to a function" , Actually a pointer to the first element is passed to the function. 当您说“将数组传递给函数”时 ,实际上第一个元素的指针将传递给函数。 This allows the called function to modify the contents of the array. 这允许被调用的函数修改数组的内容。 Since there is no copy of the array being made it makes sense to say that arrays are passed by reference. 由于没有复制数组,因此可以说数组是通过引用传递的。

Hints on how I can do this? 有关如何执行此操作的提示?

The test should be: 测试应为:

  1. Create an local array in main() . main()创建一个本地数组。
  2. Fill it with a known pattern 用已知图案填充
  3. Print the contents of array 打印数组的内容
  4. Pass the array to a function 将数组传递给函数
  5. Inside function body modify the contents of the array 在函数体内修改数组的内容
  6. Print the array inside the function 在函数内部打印数组
  7. In main() print the contents of the local array again main()再次打印本地数组的内容
  8. If outputs in 6 and 7 match. 如果67中的输出匹配。 You have a proof. 你有证明。

How do you pass an array by value? 如何通过值传递数组?

Only possible way of passing an array by value is by wrapping it in a structure. 按值传递数组的唯一可能方法是将其包装在结构中。
Online Sample : 在线样本

#include <iostream>

struct myArrayWrapper 
{
    int m_array[5];
};

void doSomething(myArrayWrapper a) 
{
    int* A = a.m_array;

    //Display array contents
    std::cout<<"\nIn Function Before Modification\n";
    for (size_t j = 0; j < 5; ++j)
       std::cout << ' ' << A[j];
    std::cout << std::endl;

     //Modify the array
     for (size_t j = 0; j < 5; ++j)
       A[j] = 100;

    std::cout<<"\nIn Function After Modification\n";
    //Display array contents
    for (size_t j = 0; j < 5; ++j)
       std::cout << ' ' << A[j];
    std::cout << std::endl;

}

int main()
{
    myArrayWrapper obj;
    obj.m_array[0] = 0;
    obj.m_array[1] = 1;
    obj.m_array[2] = 2;
    obj.m_array[3] = 3;
    obj.m_array[4] = 4;
    doSomething(obj);

    //Display array contents
    std::cout<<"\nIn Main\n";
    for (size_t j = 0; j < 5; ++j)
       std::cout << ' ' << obj.m_array[j];
    std::cout << std::endl;

    return 0; 
}

Output: 输出:

In Function Before Modification
 0 1 2 3 4

In Function After Modification
 100 100 100 100 100

In Main
 0 1 2 3 4

C only pass arguments to functions by value. C仅按值将参数传递给函数。

From the mighty Kernighan & Ritchie, 2nd edition: 摘自强大的Kernighan&Ritchie,第二版:

(1.8, Call by value) "In C all function arguments are passed by "value" (1.8,按值调用)“在C中,所有函数参数均由“值”传递

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

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