Thread: function return array

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    7

    function return array

    Well, we have two simple 1-dimensional arrays. We need to merge them inside a function and that function should return new formed(merged) array and we have to display that new array inside main () function. But without use of structures. For me problem is how does function returns array?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Pass a pointer to where you want the result stored as the 3rd parameter of the function.

    If you want to, you can return that 3rd parameter if you want.
    Just look at say strcat() which does exactly this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Pass around the pointer and size of the array (assuming it's not a string). But be careful not to declare it as an array of pointers... It's going to more than likely require the dynamic allocation of arrays (for the 3rd array at least), the FAQ should be able to help you out. Judging by your wording it's homework.

    Edit: Damn

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    7
    Thx mates. You've really helped. Solved.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    For me problem is how does function returns array?
    This is how it does

    Code:
    char * Merge(char *arr1, char *arr2)
    { ..
       return strcat(arr1,arr2);
    }
    This is only for strings.For int array u will have deal with it diff. Which zacs7 has explained

    ssharish2005

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. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM