Thread: Trouble making an Array equal a Function

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Trouble making an Array equal a Function

    ok I have been trying to figure this out on my own for the longest time and am finally giving up. Im tyring to have set an array equal to a function. Here is a simplified version of what im doing.

    #include <iostream.h>

    int test();

    int main(){

    int i;
    int t[] = test();
    for (i=0; i<9; i++) { cout << t[i]; }

    return 0;
    }


    int test() {
    int array[9];
    int i;

    for (i=0; i<=9; i++) { array[i] = i; }
    return array;
    }


    Can some tell me the "legal" way of doing this is.

    Thank You

    Matthew Ozor
    [email protected]

  2. #2
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    First off, please use [ code ] tags... second off, instead of putting

    Code:
    int test();
    all by itself, just move the function above the main() function so that the compiler understands.

    Like this:

    Code:
    #include <iostream>
    
    int test() {
    int array[9];
    int i;
    
    for (i=0; i<=9; i++) { array[i] = i; }
    return array;
    }
    
    int main(){
    
    int i;
    int t[] = test();
    for (i=0; i<9; i++) { cout << t[i]; }
    
    return 0;
    }
    \0

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    please tell us exactly what you are trying to do with this - are you attempting to get a pointer to a function so you can get bytes that the function would execute?

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    I wrote a function that randomly sorts and array of integers. I want to set an array in my main() to equal the randomly sorted array in the function.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    P.S.

    I dont want to make it a global variable that is why it is not located in outside of main. This function need to set a few variables and I want to be able to call it from a header.

  6. #6
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Why not just store it as a member function in a class?
    \0

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    #include <iostream>
    
    class RandomInts
       {
       public:
          RandomInts(int count)
             {
             m_list = new int[count];
             for(int i=0; i < count; i++)
                {
                m_list[i] = (some randomized number);
                }
             }
          ~RandomInts()
             {
             delete [] m_list;
             }
          int & operator[] (int i)
             {
             return m_list[i];
             }
    
       private:
          int
             *m_list;
       };
    
    int main(void)
       {
       RandomInts ints(9);
       for(int i=0; i < 9; i++)
          std::cout << ints[i] << std::endl;
       return 0;
       }
    if you really want to do it as a return value, you may need to dynamically allocate memory inside the function but make sure to free it in main. I don't like doing that myself.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    Thanks Salem that was too easy.. I didn't want to make a class since this needs to be portable to other programs.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Classes are portable, too.

    If you really do want to port to another program, I recommend a std::vector of ints. That way, your code works no matter how long the array is; the method already proposed requires that the array size be known to the creating method as well as the main program. Vectors are nicer.

    E.g.:

    Code:
    #include <vector>
    #include <iostream>
    using namespace std;
    
    vector<int> test (int arraySize){
      vector<int> a(arraySize);
      for (int i = 0; i < a.size(); i++) a[i] = i;
      return a;
    }
    
    int main(){
      vector<int> array = test(9);
      for (int i = 0; i < array.size(); i++)
        cout << "array[" << i << "] = " << array[i] <<endl;
    }

  10. #10
    ___
    Join Date
    Jun 2003
    Posts
    806
    cat isn't there supposed to be a return 0; in there?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    main() doesn't need a return statement. It returns 0 by default, but it still must be declared with 'int' as the return-type.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    ___
    Join Date
    Jun 2003
    Posts
    806
    Originally posted by Sang-drax
    main() doesn't need a return statement. It returns 0 by default, but it still must be declared with 'int' as the return-type.
    It brings up a warning when I compile w/out return 0; is that supposed to happen then?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by ZakkWylde969
    It brings up a warning when I compile w/out return 0; is that supposed to happen then?
    Yes. You are supposed to return from every function which has a value. Relying on assumed defaults is lazy.

    Basicly listen to your compiler. Almost always it's right and you're wrong. Warnings are there for a reason. Only poor programmers ignore them. You should always strive for a warning-free, error-free compile.

    If in doubt, compile with the "-ansi" flag.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by ZakkWylde969
    It brings up a warning when I compile w/out return 0; is that supposed to happen then?
    No. By ANSI-C++, omitting the return statement should be exactly identical in every way to having a "return 0;" at the end. It would be an error to have "return;" without a value, but omission of the return statement entirely should generate exactly equivalent code and compiler warnings as "return 0;". So it's technically an incompatibility with ANSI-C++ to give a warning.

    However, if the compiler is giving a warning, it may ALSO not be actually returning 0 either. How old/new is this compiler? I know Visual C++ .NET 2003 allows omission of the return properly; I would imagine 6.0 doesn't (6.0's ANSI compliance is terrible at best; MS's own errata page lists 15 errors in their compliance, some of them quite significant). The page doesn't actually say 6.0 has problems with the implied return from main(), though.

    Originally posted by quzah
    Yes. You are supposed to return from every function which has a value. Relying on assumed defaults is lazy.

    Basicly listen to your compiler. Almost always it's right and you're wrong. Warnings are there for a reason. Only poor programmers ignore them. You should always strive for a warning-free, error-free compile.

    If in doubt, compile with the "-ansi" flag.

    Quzah.
    Well, main() is the exception; by official ANSI-C++ standards there is no need to explicitly return; if no other return is performed, there is an implicit "return 0;". Any other function does NOT have an implicit return statement, but main() does.

    And relying on ANSI-C++ guaranteed defaults isn't lazy; do you create copy constructors, assignment operators, and destructors for classes that only do shallow copies anyway? It would be an unnecessary effort to manually recreate the automatically generated functions without a reason.
    Last edited by Cat; 07-10-2003 at 06:13 PM.

  15. #15
    ___
    Join Date
    Jun 2003
    Posts
    806
    Is it just me or is it everytime I come close to understanding this odd language someone thrws another curve ball at me completely destroying what I had to say. I still don't even know the difference in GCC and GCN (right names?)
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM