How do I make a function that accepts an array of objects as it's argument

This is a discussion on How do I make a function that accepts an array of objects as it's argument within the C++ Programming forums, part of the General Programming Boards category; I have made an array of objects. I've started by defining a class and making an array containing pointers to ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    49

    How do I make a function that accepts an array of objects as it's argument

    I have made an array of objects. I've started by defining a class and making an array containing pointers to that class type. Then I initialized each pointer to NULL.

    What I would like to do from here is pass the array to a function that will tell me how many classes are stored in the array. How would I go about doing this? Is it possible to pass an array to a function? Thanks.

    Here is my code:

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    class customer{
          public:
                customer( string );
          private:
                  string name;
    };
    
    customer::customer( string x ){
                        name = x;
    }
    
    
    
    int main(){
        customer * array[50]; //create array of pointers to customer objects
        customer fry ( "fry" );
        customer bender ( "bender" );
        
        for( int c = 0; c < 50; c++ ){ //initialize to NULL
             array[c] = NULL;
        }
        
        array[23] = &fry;
        
        return 0;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You can pass an array to a function like so:
    Code:
    void takes_array(customer array[]) {}
    customer array[9];
    takes_array(array);
    But it's really an alternative syntax for a pointer
    Code:
    void takes_array(customer *array) {}
    so you can't do a sizeof() on the "array" and get the correct results. If you want to sizeof() the array, pass the size of the array to the function:
    Code:
    #include <cstddef>  // for size_t
    
    void takes_array(customer array[], size_t size) {}
    
    takes_array(array, sizeof(array) / sizeof(*array));
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Actually arrays can't be passed to functions. When passed (by syntax), they will be converted to a pointer automatically. This is why sizeof() wont work there.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    An array dimension should be initialized from a constant expression.

    Instead of customer* array[50] you should instead consider the following:

    Code:
    const int size = 50;
    customer* array[size];
    It is then a matter of passing size as an argument to your function(s).

    By passing an array to a function you still cannot ensure when the array ends. You will still have to provide the function with the array size. the number 50 by itself is a so-called magic-number and it will give you nothing but grief.

    By making it a constant expression you make the definition of size central to one location. If later you need to change the size of the array, you just need to change this variable. You will not need to go to all the places where the array is being used and chenge every instance of 50 to the new dimension.

    To pass your array to a function:

    Code:
    return_type function_name(customer* array, const int size)
    where customer* array is in fact a pointer to the first position in the array. size is the size of the array. You have now all elements to properly handle the array while checking for out of bounds.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If you search the board you turn up many threads/debates on the topic. One such thread is Passing an array into a function
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    In C++ you would normally use a vector which keeps track of its own size. Also, with a vector you can just push_back the customers that exist. If you need the customer to be at a certain index (like 23), then a map might also be appropriate.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    By making it a constant expression you make the definition of size central to one location. If later you need to change the size of the array, you just need to change this variable. You will not need to go to all the places where the array is being used and chenge every instance of 50 to the new dimension.
    There's no need for an array dimension to be a constant size.
    Code:
    int *array = new int[lines_the_user_requested];
    But either way, you can't determine the size of an array with sizeof() if that array has been passed to a function . . . you'll get the size of the pointer. It needs to be passed to the function unless you use a magic number or a global constant of some kind.

    I agree that you shouldn't use "magic numbers" in your code like 50, though . . . unless you like tedious searching and replacing.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    > There's no need for an array dimension to be a constant size.

    It does when the array is not being dynamically allocated, which was the case at hand. There's also of course the situation where an array is initialized explicitly as in:

    Code:
    int array[] = {1, 2, 3};
    But then and again no dimension is being given.

    After C99 gets incorporated into C++, then there will be no longer this requirement.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    Actually, I will eventually need to make a dynamic array of objects.

    I was hoping to create a function that would take the array and find the nearest empty slot with which to place the next object. That was what I was hoping to turn the function into. I figured first that it would have to accept an array.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Actually, I will eventually need to make a dynamic array of objects.
    Then you really should use vector. A dynamic array should always be passed by pointer, and you have to pass the size explicitly along with it.

    BTW, you #include <cstring> but you use the C++ string class. The correct header should be <string>.

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by rakan
    Actually, I will eventually need to make a dynamic array of objects.

    I was hoping to create a function that would take the array and find the nearest empty slot with which to place the next object. That was what I was hoping to turn the function into. I figured first that it would have to accept an array.
    Use std::vector and push_back() the new entries.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Use std::vector and push_back() the new entries.
    std::vector: http://cppreference.com/cppvector/index.html
    std::vector.push_back(): http://cppreference.com/cppvector/push_back.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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