Thread: Passing array by reference

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    Passing array by reference

    Hi. I'm stuck on something that seems quite simple: passing an array by reference. I've attached a skeleton version of the code which gets the idea across. Array num[2] is passed to process_day but process_day can't see the values in the array.

    What am i missing here?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So, is this C or C++
    Because there's some of each in there.
    I'm going with C++

    From header.h, remove
    #include <stdio.h>

    From process_day, delete these 2 lines
    > int a[2];

    > int b;

    And use cout instead of printf()

    In main
    > process_day(&num[2], num2);
    Should be
    process_day(num, num2);
    But num2 is neither initialised or used, so it can be removed as a parameter.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    2
    Awesome, thanks for the quick response.

    I learned C back in college 15 years ago but the compiler i'm using, Dev-C++ is defaulted to C++...thus the mixing. The message boards and online tutorials seem to indicate this is acceptable, you agree?

    Tom

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    arrays are ALWAYS passed by reference. all you have to do is place the name of the array in the parameter list, without the '&' reference operator.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > The message boards and online tutorials seem to indicate this is acceptable, you agree?
    I personally don't care whether you use C or C++.
    But I would suggest you make a clean choice rather than some random hybrid which randomly mixes both.

    You can make dev-c++ default to C if you want it to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  4. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 05:19 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM