When getting array as a parameter for a function, only one element is passed

This is a discussion on When getting array as a parameter for a function, only one element is passed within the C++ Programming forums, part of the General Programming Boards category; I'm trying to pass an array in a function, but only one element gets through. For example, in this program, ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    When getting array as a parameter for a function, only one element is passed

    I'm trying to pass an array in a function, but only one element gets through. For example, in this program,
    Code:
    #include <iostream>
    using namespace std;
    
    void displayInts(int intergers[]);
    
    int main()
    {
    	int intergers[4];
    
    	intergers[0] = 1;
    	intergers[1] = 2;
    	intergers[2] = 3;
    	intergers[3] = 4;
    
    	displayInts(intergers);
    
    	system("PAUSE");
    }
    
    void displayInts(int intergers[])
    {
    	int size = (sizeof(intergers) / sizeof(int));
    
    	for(int i = 0; i < size; i++)
    	{
    		cout<<intergers[i]<<endl;
    	}
    }
    the output is:

    Code:
    1
    Press any key to continue...
    How can I fix this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,827
    Quote Originally Posted by bijan311
    I'm trying to pass an array in a function, but only one element gets through.
    Actually, none of the elements get through. What does get passed is a pointer to the first element of the array. Using that pointer, you can access the various elements of the array. Hence, the sizeof approach only gives you the size of a pointer (which you then divide by the size of an int, which does not make a great deal of sense).

    Quote Originally Posted by bijan311
    How can I fix this?
    You could use a container instead, or maybe std::array<int, 4>. The standard containers have a size() member function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    When an array is passed to a function it "decays" to a pointer, losing any size info. So you can't determine its size using sizeof. You need to pass a second parameter with the size.

    BTW, integer only has one r.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,868
    try this:

    Code:
    template<int size>
    void displayInts(int (&intergers)[size])
    {
        for(int i = 0; i < size; i++)
        {
            cout<<intergers[i]<<endl;
        }
    }
    keep in mind that this will only work if your array is a true array, and not if it's a pointer to a block of memory allocated with new or by some other means.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Thanks to everyone, now my problem is solved.

    BTW, integer only has one r.
    Well, I feel a little silly now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How passed as an input parameter?
    By Siaw Ys in forum C Programming
    Replies: 3
    Last Post: 12-03-2011, 05:06 AM
  2. Object passed to method as parameter
    By elodman in forum C# Programming
    Replies: 11
    Last Post: 09-22-2011, 07:59 PM
  3. Dynamic Array Passed to a function by Reference
    By patriots21 in forum C Programming
    Replies: 7
    Last Post: 11-21-2010, 10:32 AM
  4. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  5. Comparing A Struct To A Passed Parameter
    By Anonymous in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2002, 10:38 PM

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