Thread: question about arrays

  1. #1
    Registered User helloalyssa's Avatar
    Join Date
    Sep 2010
    Posts
    25

    question about arrays

    hi everyone. i'm reviewing for my final and my teacher posted this as a sample final question.


    What would the following block of code output?
    Code:
    #include <iostream>
    using namespace std;
    void fun1(int w, int& x, int a[]);
    
    int main()
    {
      int a = 1, b = 5;
      int c[7];
      c[0] = 10;
      fun1(a, b, c);
      cout << "\nMain: a=" << a << " b=" << b << " c[0]=" << c[0];
      return 0;
    }
    void fun1(int w, int& x, int a[])
    {
      cout << "\nFun1: w=" << w << " x=" << x << " a[0]=" << a[0];
      w = 100;
      x = 200;
      a[0] = 300;
    }
    the answer is:
    Code:
    Fun1: w=1 x=5 a[0]=10
    Main: a=1 b=200 c[0]=300

    i'm not quite clear on why the answer is what it is and it'd be great if someone could explain to me the answer. thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Do you have a question about #1
    Fun1: w=1 x=5 a[0]=10
    or #2
    Main: a=1 b=200 c[0]=300
    Or both?

    Edit: Look at your function what is different about the way each variable is passed?

    Jim
    Last edited by jimblumberg; 12-12-2010 at 10:42 PM.

  3. #3
    Registered User helloalyssa's Avatar
    Join Date
    Sep 2010
    Posts
    25
    i'm asking about both outputs.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    To repeat what Jim said:

    Look at your function what is different about the way each variable is passed?

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by helloalyssa View Post
    i'm asking about both outputs.
    When you pass a variable by reference and pointer, you can change the value of the variable in the function. When you pass by value, you are only working with copy of the variable, thus changing its value in the function won't affect the variable.

    so in your function, when you call the function fun1(a,b,c) you pass b and c by reference and pointer respectively. "a" is passed by value.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you need to be a little more specific about what you don't understand. What do you think the output will be and why?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM