1

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?

Community
  • 1
  • 1
Colleen
  • 23,899
  • 12
  • 45
  • 75
  • [This may help, it shows an array wrapped in a struct](http://www.velocityreviews.com/forums/t288620-pass-array-by-value.html) – David B Jun 22 '12 at 15:06
  • 2
    Why the downvote? It's not a dumb assignment, Colleen asks questions that show some understanding and it is clearly marked as homework. – Jens Jun 22 '12 at 15:20

2 Answers2

5

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
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • This answer is super incomplete and doesn't make any sense grammatically, sorry. – Colleen Jun 22 '12 at 15:07
  • This simply proves what we already know, we pass an array by pointer. – David B Jun 22 '12 at 15:08
  • 1
    @Colleen: How is this Super Incomplete? And Sorry I can't make it any better gramatically. – Alok Save Jun 22 '12 at 15:12
  • Ah, you edited it (when I first saw it you had 2 sentences). It's more complete now, but it still doesn't explain to me, if it's passed by reference, how an array can be passed by value. (btw grammatically: modifies, not modify's, and your first sentence is a fragment-- should be "since <...>, you can say <...>". I mention the grammar because it's important for actually being able to understand your answer-- I initially had to reread it like 3 times before I even understood what you were trying to say) – Colleen Jun 22 '12 at 15:17
  • @Colleen: I made it short and simple hope that helps. – Alok Save Jun 22 '12 at 15:31
  • so how would it be passed by value? – Colleen Jun 22 '12 at 15:44
  • @Colleen: Edited the answer to address that. – Alok Save Jun 22 '12 at 15:53
  • @Als it is tagged as C :) She hasn't deal with C++ yet! ;) – A_nto2 Jun 22 '12 at 16:12
  • @A_nto2: Hmm, Yes but Just need to replace the `cout` with `printf` the rest of the answer remains the same. – Alok Save Jun 22 '12 at 16:13
2

C only pass arguments to functions by value.

From the mighty Kernighan & Ritchie, 2nd edition:

(1.8, Call by value) "In C all function arguments are passed by "value"

ouah
  • 142,963
  • 15
  • 272
  • 331
  • but what does that mean in the context of arrays? If passing by pointer is passing by value for an array, what does it mean to pass by reference? – Colleen Jun 22 '12 at 15:10
  • 2
    In the C context when people say pass by reference, they mean passing a pointer by value. C does not have pass by reference contrary to C++. – ouah Jun 22 '12 at 15:12
  • what do you mean "passing a pointer by value"? Sorry, I'm new to C and pointers hurt my head. Can you give me a concrete example? – Colleen Jun 22 '12 at 15:13
  • `void foo(int *p); void bla(void) { int b = 42; foo(&b); }` Here the pointer argument `&b` is passed by value. Some people incorrectly say you are passing the `int` object `b` by reference. – ouah Jun 22 '12 at 15:15
  • can you explain it in the context of an array? And show me both ways? I edited my answer with an example. – Colleen Jun 22 '12 at 15:20
  • `void bar(int a[16]);` is equivalent in C to `void bar(int *a);` You a re passing a pointer to the first element of the array when you call the function. You cannot pass arrays to functions in C, only a pointer to their first element. – ouah Jun 22 '12 at 15:25