Thread: Returning Arrays as pointers

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Returning Arrays as pointers

    Ok i have a function that returns an address to an array of ints which contains 4 items. How do I assign the contents of that address to a local variable.

    The return statements is this return &date;

    I want to copy the contents from this address into a new local array.


    Thanks.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You could use the memcpy() function.

    Something to look out for: If the array that you're returning is local to that function, then it is invalid before it gets returned to the calling function. You can't use the pointer in the calling function to get to the data unless you declare the array in the function as static.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    Quote Originally Posted by itsme86 View Post
    You could use the memcpy() function.

    Something to look out for: If the array that you're returning is local to that function, then it is invalid before it gets returned to the calling function. You can't use the pointer in the calling function to get to the data unless you declare the array in the function as static.
    Yah I forgot about that. I how would I pass that back then. The function accepts a string splits it up into integers and puts it into an array. Now, I need that array but I am not supposed to use static or global variables.

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    If inside the function, you use malloc() to allocate the necessary space for the array, then it is up to you to free that space, which means that you can safely return an address to that space from within your function. Something like this:
    Code:
    int* myFunction()
    {
    int *myArray = malloc(4 * sizeof(int));
    return myArray;
    }
    
    int main()
    {
    int myArray[4];
    int *p = myFunction();
    memcpy(myArray, p, 4 * sizeof(int));
    }

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Can you declare the array you are supposed to return elsewhere in the program?
    If so, you can also just pass a pointer to that array as an argument, work with it in your function, and return it back.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The standard way of doing this type of thing is to allocate the array outside the function, pass a pointer to the array to the function, and let the function write into it. This is also more efficient than what you're trying to do since it avoids the extra copy.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > The standard way
    There is nothing standard about it. Don't confuse the standard way with the simplest way.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > There is nothing standard about it. Don't confuse the standard way with the simplest way.
    Well, besides being simple, it's also as efficient as possible (since it avoids the extra copy) and makes it easier to match up malloc() and free(), since both are outside the function (unlike calling malloc() inside the function). It also allows more control over memory since one can allocate a large contiguous array outside and pass pointers to pieces of it to the function. So I think it's reasonable to call it "standard" in the sense that it's what most good programmers choose to use. I didn't mean that it was part of some formal standard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 03:38 PM
  2. Pointers to 2D arrays
    By bertazoid in forum C Programming
    Replies: 16
    Last Post: 02-26-2009, 04:46 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM
  5. Hello all and pointers to multi-dimensional arrays
    By Mario in forum C++ Programming
    Replies: 16
    Last Post: 05-17-2002, 08:05 AM