Thread: return array position from function

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    46

    return array position from function

    Hey, I'm trying to return the position of the array so I can print the contents of a structure at that position, I run in to the problem of what if the array position is 0? This is a problem cause I also want to return 0 for false. How do I distinguish?

    Code:
    int find_jock(struct athlete players[], int jock, int top) {
     int i = 0, found = 1;
    
    while (found == 1 && i < top) { 
      if (jock == players[i].rank) 
        return i;
         /* end if */
      i++; }
    return 0;
    Code:
    display_jock(players[find_jock(players, 1, top)]);

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    return -1 if it fails

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by danieldcc
    Hey, I'm trying to return the position of the array so I can print the contents of a structure at that position, I run in to the problem of what if the array position is 0? This is a problem cause I also want to return 0 for false. How do I distinguish?
    There are a few options available. For example:
    • Return a pointer to the element found, then use a null pointer to denote not found.
    • Return the index, but return the index one past the last index to denote not found.
    • Return the index, but return -1 to denote not found. (This works even if you are using size_t as the return type.)
    • Make use of an output parameter, e.g., a pointer to int. This could be set to denote found/not found, or set to "return" the index to the caller, depending on the design you prefer.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to return an array from a function
    By Werewolfkiss in forum C++ Programming
    Replies: 11
    Last Post: 04-08-2009, 11:03 AM
  2. Can function return an array?
    By bulletbutter in forum C++ Programming
    Replies: 21
    Last Post: 04-17-2008, 01:45 PM
  3. function return array
    By dukysta in forum C Programming
    Replies: 4
    Last Post: 06-10-2007, 09:49 AM
  4. return array from function
    By beginner.c in forum C Programming
    Replies: 11
    Last Post: 04-26-2007, 05:51 AM
  5. Return an array from function
    By mozart in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2006, 12:54 AM