Like Tree1Likes
  • 1 Post By Salem

Passing Array to a Function

This is a discussion on Passing Array to a Function within the C++ Programming forums, part of the General Programming Boards category; take a look Code: #include <iostream> using namespace std; void input (int test[5]) { int i; for(i=0;i<5;i++) { cout<<"Enter a ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    Passing Array to a Function

    take a look
    Code:
    #include <iostream>
    using namespace std;
    
    void input (int test[5])
    {
    int i;
    for(i=0;i<5;i++)
    {
    cout<<"Enter a number: ";
    cin>>test[i];
    }
    }
    int main()
    {
    int array[5];
    input(array);
    for(int i=0;i<5;i++)
    {
    cout<<array[i]; //shows the same values that i have input using the //function
    cout<<endl;
    }
    return 0;
    }

    I am passing array to function but it is being passed by reference..Could you please explain this concept?
    Why is this being passed as reference even though i am not using & operator to pass it as reference. I wrote a bubble sort algorithm using the same concept and i passed the array to function and did not return anything but it sorted the array in my main function which is weird as i did not pass the array by reference. I passed it like in the above program.
    Could you guys please explain this?I've been trying to find answer on Google but haven't been able to find it yet.Hopefully you guys can help.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Arrays are always passed as a pointer to the first element.

    It's as if you did
    input( &array[0] );

    Which happens regardless of whether you declare your function as
    void input (int test[5])
    or
    void input (int test[])
    or
    void input (int *test)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    2
    Quote Originally Posted by Salem View Post
    Arrays are always passed as a pointer to the first element.

    It's as if you did
    input( &array[0] );

    Which happens regardless of whether you declare your function as
    void input (int test[5])
    or
    void input (int test[])
    or
    void input (int *test)
    Thank you for the explanation.So it is basically passing the address of the first element of the array to the function and then function is able to increment to next element address?..Also, I wrote this little function for my program.Could you please explain what this would return and whether i should remove this line?

    Code:
    int randomfunc (int random[3][3])
    {    
        int rno=5;                                        
        for(row=0;row<3;row++)                                    for(column=1;column<3;column++){                
                random[row][column]=rno%10;
        rno=5+3;
            }
    
        return random[3][3]; //What would this return?
    }
    void main()
    {
    int array[3][3];
    array[3][3]=randomfunc(array);
    }
    It does seem to be producing the correct output (i.e my array now contains random numbers..but could you please explain what would be returned by return random[3][3] and whether i should include it or not?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    I dunno, I was encouraged by your first post with int main, despite your username.

    Sadly, post #3 regresses back to the dark ages.

    > return random[3][3]; //What would this return?
    Garbage.
    The max valid subscript for your array is random[2][2], so you're well off into the weeds with this one.

    The assignment in main isn't any better.
    iMalc likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    So first, change back to int main. SourceForge.net: Void main - cpwiki
    And secondly, what you are trying to achieve? Returning an array?
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Salem View Post
    Which happens regardless of whether you declare your function as
    void input (int test[5])
    or
    void input (int test[])
    or
    void input (int *test)
    Just to highlight that the size is actually not checked if you use the first so there is no actual reason to put it. It matters for a 2D array where you need to put the "minor" dimension like "[][X]".

    This also means that the function won't know the exact size of the array and that can be problematic. As you saw, you were accessing [3][3] which is invalid. Well, even if you do [2][2] then if the input is actually a 1x2 array then you can just create a buffer overflow if the function is used for a user input for example.

    That is why std::vector (and std::array in C++11) are preferred.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-25-2012, 01:31 AM
  2. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 12:32 PM
  3. Passing array into function
    By kulfon in forum C Programming
    Replies: 2
    Last Post: 11-30-2010, 10:08 PM
  4. passing an int array into function
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2004, 04:15 AM
  5. passing 2d array in to function
    By Kings in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2003, 10:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21