dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: how to pass an array to a function?

  1. #1
    Join Date
    Jan 2010
    Posts
    57

    how to pass an array to a function?

    example:

    if I have in main:

    Code:
    void te()
    {
    
    
    }
    
    int main()
    {
               int b[] = (2, 34, 4);
    }

    how you pass array b to function te? THX
    Last edited by Johnseito; May 14th, 2010 at 08:21 PM.

  2. #2
    Join Date
    Jan 2010
    Posts
    57

    Re: how to pass an array to a function?

    Code:
    void te(int b[])
    {
    
    
    }
    
    int main()
    {
               int b[] = (2, 34, 4);
    }

    how do you pass the array and also get the array length to the function? THX

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    Re: how to pass an array to a function?

    Code:
    void te(int *p, int array_size)
    {
        cout << array_size;
    }
    
    int main()
    {
        int b[] = (2, 34, 4);
        te(b, 3);
    }
    or better yet

    Code:
    void te(vector<int>& arr)
    {
        cout << arr.size();
    }
    
    int main()
    {
        vector<int> myVector;
        myVector.push_back(2);
        myVector.push_back(34);
        myVector.push_back(4);
        te(myVector);
    }

  4. #4
    Join Date
    Jan 2010
    Posts
    57

    Re: how to pass an array to a function?

    do you know why this is not possible?

    Code:
    void te(int *p, int array_size)
    {
        cout << array_size;
       
        int ar[array_size];  //ERROR with this, why?
    }
    
    int main()
    {
        int b[] = (2, 34, 4);
        te(b, 3);
    }

    I put this code:

    Code:
    void te(vector<int>& arr)
    {
        cout << arr.size();
    }
    
    int main()
    {
        vector<int> myVector;
        myVector.push_back(2);
        myVector.push_back(34);
        myVector.push_back(4);
        te(myVector);
    }
    and it is wrong, there are errors, do you know why? Thx

  5. #5
    Join Date
    Aug 2008
    Posts
    902

    Re: how to pass an array to a function?

    1. You can't initialize an array with a variable like that. You need to use int *ar = new int[array_size]
    2. You need to include the right headers. vector

  6. #6
    Join Date
    Jan 2010
    Posts
    57

    Re: how to pass an array to a function?

    so instead of pass the array size,

    why can't you do?

    Code:
    arrayLength =sizeof(b)/sizeof(b[0]);
    this code is after pass of array to the function. This works in main but not when pass to function.

  7. #7
    Join Date
    Aug 2008
    Posts
    902

    Re: how to pass an array to a function?

    Quote Originally Posted by Johnseito View Post
    so instead of pass the array size,

    why can't you do?

    Code:
    arrayLength =sizeof(b)/sizeof(b[0]);
    this code is after pass of array to the function. This works in main but not when pass to function.
    It works in main because your compiler is smart. However, the compiler isn't omnipotent. All you passed to the function is a pointer. In C# or Java you could find the size of an array because an array is actually an object in those languages. In C/C++, an array is nothing more than a pointer to values in memory and it's up to you to remember and manage its size. This is why vector exists, and this is the reason you should use it. There is also std::array, if your compiler supports it.
    Last edited by Chris_F; May 14th, 2010 at 10:35 PM.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: how to pass an array to a function?

    Quote Originally Posted by Johnseito View Post
    and it is wrong, there are errors, do you know why? Thx
    You probably failed to #include<vector> and/or didn't specify to use the std namespace.

  9. #9
    Join Date
    Mar 2006
    Posts
    149

    Re: how to pass an array to a function?

    how do you pass the array and also get the array length to the function? THX
    Here's a clean way to do it, modifying your original example:

    Code:
    template <unsigned int nElements> void te(int (&b)[nElements])
    {
        for (int i = 0; i < nElements; ++i)
        {
            cout << b[i] << endl;
        }
    }
    
    int main()
    {
        int b[] = {2, 34, 4};
        te(b);
    }
    This is the mechanism used by the "safe" functions such as strcpy_s.
    Last edited by GeoRanger; May 15th, 2010 at 10:14 PM. Reason: "nSize" is ambiguous. Use "nElements" instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)