Thread: Arrays and Functions

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    12

    Arrays and Functions

    I'm kinda concerned about not being able to pass arrays to functions.

    Would be just be better to make the array global?

    here's an example.

    I'm making a text based tic-tac-toe game.

    I wanted to have functions for the follow things:

    1. initialize the array.
    2. Updating the array on player's 1 turn
    3. Updating the array on player's 2/computer's turn

    but now I have to put them all in main. I kinda don't like that and thought it would be better to have functions to do these things.

    So I ask again, would it just be better to make the array global?

  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
    Of course you can pass arrays to functions.

    Code:
    void foo ( int a[] ) {
      a[0] = 0;
    }
    
    int main ( ) {
      int arr[10];
      foo ( arr );
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    when you pass an array to a function it's not an actual array, it's a pointer to the first item in the array passed in. so doing:

    Code:
    void foo(int a[]) {}
    is the same as doing:
    Code:
    void foo(int *a) {}

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    ....and your point is?

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Perspective
    ....and your point is?
    Um.. o.o ..Seems like hes pointing out something thats mentioned in 95% of the C++ tutorials out there, how strange that is! not?

    Which would mean you can operate on that pointer arithmatically too, a + 5.

    Avoid anything global as much as possible.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    lol, Dae. I think you should put a little disclaimer in your sig that says: "Any of my comments are not meant to incite rage or anger".
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    Quote Originally Posted by Dae

    Which would mean you can operate on that pointer arithmatically too, a + 5.
    so if I understand you correctly then this should be legal...

    Code:
    for (int x=0;x<10;x++)
    {
        a+x="*" //assuming the array is string
       
        return a
    }
    Last edited by KunoNoOni; 10-03-2005 at 09:13 PM.

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    KunoNoOni:
    That makes absolutely no sense.
    For starters, why put a return statement in a for loop like that? It won't even loop it will get to the end of the block and 'return'.
    a+x="x" makes no sense:
    1. what is 'a'?
    2. "*" should be '*'
    3. If 'a' was an int then 'a' would return the ascii value of '*'

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > so if I understand you correctly then this should be legal...
    Did you try and compile it first?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    I'm not at home right now... so no I haven't compiled it yet.

    As far as my example, I don't know what I was thinking about when I put the return in the loop . 'a' is the array which is a string.

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    Ok I think I figured it out. Thank for all the help!! ^_^

  12. #12
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    Quote Originally Posted by Dae
    Um.. o.o ..Seems like hes pointing out something thats mentioned in 95% of the C++ tutorials out there, how strange that is!
    sorry, never bothered to read any c++ tutorials on the 'net, when I learned there wasn't much out there for tutorials.

  13. #13
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by rockytriton
    sorry, never bothered to read any c++ tutorials on the 'net, when I learned there wasn't much out there for tutorials.
    Why are you saying sorry to me? I was being sarcastic at Perspective's odd comment to your help.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM
  5. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM