c通过值传递数组? [英] c pass array by value?

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

问题描述

我有这样的家庭作业问题:

A部分

编写一个测试是否将以下数据类型按引用或按值传递的一个C程序,并打印它发现出来到终端:


  • INT

  • int型数组

可以想像,如果你的程序发现一个int是按值传递和int数组是按值传递,那么它应该产生的输出,如:
 INT:通过按值
型数组:通过按值

的int我理解,但我不明白的是:


  1. 我想传递一个数组的唯一方法是通过第一个值的地址在数组

  2. 执行此计为按值传递或按引用传递? (困惑我值传递数组给一个函数)

  3. 在我怎么能做到这一点提示?我假设像传递引用另一个函数,它操纵,并看它是否改变了,但我不知道......

编辑:可能有帮助,如果有人解释这个给我一个具体的例子,如:说我有存储在存储器位置0长度为10的int数组(是的,我知道,并不是真实的生活,但为求的例子...)。它会是什么样如果阵列通过引用传递什么样的?它看起来什么,如果它是按值传递什么样的?


解决方案

  

请问这算不算按值传递或引用传递?


当你说的将数组传递给函数的,其实一个指向第一个元素传递给函数。这允许被调用的函数来修改所述阵列的内容。由于没有被毫不掩饰对数组的副本,它是有道理的说,数组通过引用传递。


  

我如何能做到这一点的提示


测试应该是:


  1. 创建于的本地阵列的main()

  2. 与已知的图案填充它

  3. 打印数组的内容

  4. 的数组传递给函数

  5. 在函数体中修改数组
  6. 内容
  7. 打印阵列的功能在

  8. 的main()再次打印本地数组的内容

  9. 如果在 6 输出和 7 匹配。你有证明。


  

如何按值传递数组?


由值传递数组唯一可能的方式是通过在一个结构包裹它。结果
在线样品

 的#include<&iostream的GT;结构myArrayWrapper
{
    INT m_array [5];
};无效DoSomething的(myArrayWrapper一)
{
    为int * A = a.m_array;    //显示数组的内容
    性病::法院LT&;<\\ n在函数修改之前\\ n;
    为(为size_t J = 0; J&小于5 ++ j)条
       性病::法院LT&;< ''<< A [J]。
    性病::法院LT&;<的std :: ENDL;     //修改数组
     为(为size_t J = 0; J&小于5 ++ j)条
       A [D] = 100;    性病::法院LT&;<\\ n在函数修改\\ n后;
    //显示数组的内容
    为(为size_t J = 0; J&小于5 ++ j)条
       性病::法院LT&;< ''<< A [J]。
    性病::法院LT&;<的std :: ENDL;}诠释的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);    //显示数组的内容
    性病::法院LT&;<\\ n在主\\ n;
    为(为size_t J = 0; J&小于5 ++ j)条
       性病::法院LT&;< ''<< obj.m_array [J]。
    性病::法院LT&;<的std :: ENDL;    返回0;
}

输出:

 在函数修改前
 0 1 2 3 4在功能修改后
 100 100 100 100 100在主
 0 1 2 3 4

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:

  • 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

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...). 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().
  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
  8. If outputs in 6 and 7 match. 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通过值传递数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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