Thread: return array

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    12

    return array

    Hi ..
    We were given an assignment to write a function called split.. it splits a string based on a certain delimiter and adds each split in an array then returns that array.
    I am not trying to have it solved for me, I just have a couple of questions regarding returning arrays.

    first I tried to return the array name which is obviously its address. the compiler gave a warning but i recieved the correct output.

    the second thing i tried was to declare the array as static. no warnings given and the output is correct.

    the third approach is to have the function work with a global array.

    What is the best approach and is there another way I could do it.
    I suppose I could just do it in C++ and have the function return a Vector.. but I would like to know more about how programmers go about that in C.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mark707
    first I tried to return the array name which is obviously its address. the compiler gave a warning but i recieved the correct output.
    What was the warning? What do you understand of the warning?

    Quote Originally Posted by mark707
    the second thing i tried was to declare the array as static. no warnings given and the output is correct.
    This can work, but then it means that there will be pitfalls that must be avoided when calling the function more than once.

    Quote Originally Posted by mark707
    the third approach is to have the function work with a global array.
    This is a poor solution since it means that your function can only be used with a single array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think you can only return a pointer and not an array but I could be wrong.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    12
    What was the warning? What do you understand of the warning?
    It warned me about returning address of a local variable.
    Last edited by mark707; 10-14-2013 at 02:20 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mark707
    It warned me about returning address of a local variable.
    That means that you declared the array to be local to the function. As such, returning a pointer to the first element of the array is wrong: once the function returns, the array no longer exists. The fact that it worked for you does not mean that it is correct; the behaviour here is undefined.

    One observation I make is that the number of tokens resulting from the split is likely to be unknown in advance. As such, you might make use of dynamic memory allocation. If so, then instead of declaring a local array, you would have a local pointer that you can return. The caller would be responsible for calling free.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by mark707 View Post
    first I tried to return the array name which is obviously its address. the compiler gave a warning but i recieved the correct output.
    As laserlight hinted, the compiler gives you a warning for a reason. Strictly speaking, any attempt to use a local array returned in this way gives undefined behaviour. The problem with undefined behaviour is that ANY result is permitted. One permitted result is giving you the result you expect. Another permitted result is crashing your program. Yet another permitted result is reformatting your hard drive. The problem is that your code may give behaviour you expect for you, but give some other behaviour for someone else (for example, someone using a different version of your compiler to build your code). Because ANY behaviour is correct. And it is the code that is incorrect.

    Quote Originally Posted by mark707 View Post
    What is the best approach and is there another way I could do it.
    There is no "best" approach in C. There are options. Your first option is a really bad one (because undefined behaviour is something to be avoided, except in very specific settings - and you have given no information to suggest you are working in such a setting).

    You need to think about things more holistically though. The goal is to return data reliably from your function, for the caller to be able to use it.

    What limitations can you reasonably expect the caller (or the programmer writing calling code, who may or may not be you) to accept?

    What do you expect to happen if your function is called more than once? If your function is called twice, can it return the same data both times, or will it return something different?

    If your function returns the same address each time, does it write different data to it on subsequent calls? If it does, can the caller be relied on to keep copy of older data in order to safely use it?

    Imagine what a statement like this
    Code:
       some_caller_function(your_function(), your_function());
    will do if your_function() returns different data each time, or if the second call of you_function() returns the same address but writes different data to it. Can you rely on the caller to avoid this? Or not?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    yup pointers are the best solution for you.

  8. #8
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Code:
    /* count_tokens: Computes the number of tokens in 's'. */
    size_t count_tokens(const char *s);
    
    /* copy_tokens: Splits 's' into multiple strings and assigns a pointer to each one in '*a'. Terminates with a null pointer. */
    char **copy_tokens(char **a, char *s);
    
    /* tokenise: Makes a copy of s, allocates the necessary storage for the array, stores each token in the array, and returns it (using both of the functions listed above). */
    char **tokenise(const char *s);
    If you have a complicated tokenisation process, as you may if you were writing a complicated line-parser for a shell, you could write a finite state machine in a generic manner that would allow you to use it with both count_tokens and copy_tokens, leading to a simple implementation for both of those functions.

    The tokenise function really only requires two calls to malloc. If whitespace may be present before the tokens, you can allocate an extra pointer, store it in the first element, then return a pointer to the second element of the array. That will allow you to free the copy of 's' later on.
    Last edited by Barney McGrew; 10-14-2013 at 03:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return 2D array?
    By fcommisso in forum C Programming
    Replies: 2
    Last Post: 10-07-2009, 09:32 PM
  2. How do i return an array?
    By Fredir in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2007, 11:57 AM
  3. How do I return an Array?
    By Mr. Flibble in forum C++ Programming
    Replies: 17
    Last Post: 10-06-2006, 04:37 AM
  4. Return an array
    By spaghettihoop5 in forum C Programming
    Replies: 4
    Last Post: 07-07-2006, 12:48 PM
  5. Return array?
    By antex in forum C++ Programming
    Replies: 3
    Last Post: 06-02-2005, 02:29 PM