[英]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程序,以测试以下数据类型是通过引用还是通过值传递,并将发现的结果打印到终端:
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: 我了解的诠释,但我没有得到的是:
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: 测试应为:
main()
. 在main()
创建一个本地数组。 main()
print the contents of the local array again 在main()
再次打印本地数组的内容 6
and 7
match. 如果6
和7
中的输出匹配。 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.