Thread: returning an array from a function

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    returning an array from a function

    Hi, I think that I am having some conceptual problems with pointers. I have an Integer Array being created and used in a function, and I would like to return the array back into my main().

    This would seem easy, but in the books they speak of using pointers instead of my array. Do I need to change it to a pointer and then return it? Please tell me what I have done wrong.

    This is a sample program that does the same thing as I want.

    Code:
    int function()
    {
         int x1[4];
         int a;
         for(a = 0; a < 4; a++)
         {
              x1[a] = a;
         }
         return x1;
    }
    
    int main()
    {
         int x2[4];
         x2 = function();
         return 0;
    }
    Thanks a lot for your time!

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    5
    Of Course! Thanks a lot, I really appreciate your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. c++ -- returning an array from function
    By p4plus2 in forum C++ Programming
    Replies: 25
    Last Post: 08-18-2008, 01:48 PM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 6
    Last Post: 10-21-2003, 09:57 PM