Thread: How to return two elements of an array

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    How to return two elements of an array

    I would like to return both elements of n[].
    I cant find out how to do it.

    Code:
    short* GetPosition(char letter, short number)
    {
         short n[2] = {0,0};
    
         if     (letter == 'a' && number == 1) n[] = {7,  3};
         else if(letter == 'a' && number == 2) n[] = {7, 10};
         else if(letter == 'a' && number == 3) n[] = {7, 17};
         else if(letter == 'a' && number == 4) n[] = {7, 24};
         else if(letter == 'a' && number == 5) n[] = {7, 31};
         else if(letter == 'a' && number == 6) n[] = {7, 38};
         else if(letter == 'a' && number == 7) n[] = {7, 45};
         else if(letter == 'a' && number == 8) n[] = {7, 52};
    
         return n[0, 1]; 
    }
    Last edited by Ducky; 03-28-2009 at 10:37 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, what is n? It looks like you have incorrect syntax.

    Chances are, you want to return a std::pair<int, int>, or some struct or class object.
    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 VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That doesn't look like valid code.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    I edited the code, changed the return value from char* to short*.

    n[2] is an array of two elements.

    Chances are, you want to return a std:air<int, int>, or some struct or class object.
    You mean i cant return both elements of an array?
    Last edited by Ducky; 03-28-2009 at 10:38 AM.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, you would then be returning a pointer to the first element of a local array, which means that the caller would get a pointer that points to something that no longer exists, which is a Bad Thing. Plus, you still have invalid syntax, e.g.,
    Code:
    n[] = {7,  3};
    One way of doing what you want is to #include <utility> and use std::pair<short, short> with std::make_pair, e.g.,
    Code:
    std::pair<short, short> GetPosition(char letter, short number)
    {
        using std::make_pair;
        if     (letter == 'a' && number == 1) return make_pair(7, 3);
        else if(letter == 'a' && number == 2) return make_pair(7, 10);
        // ...
    }
    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
    Dec 2007
    Posts
    932
    Thank you Laserlight!

    Code:
    n[] = {7,  3};
    May i ask why would this be invalid? Im assigning values to an array.
    Using Windows 10 with Code Blocks and MingW.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    May i ask why would this be invalid? Im assigning values to an array.
    ... which is invalid. You cannot assign to an 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

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Arrays

    "When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty . In this case, the compiler will assume a size for the array that matches the number of values included between braces

    Sorry i dont get it. This is what i did.
    Last edited by Ducky; 03-28-2009 at 11:21 AM.
    Using Windows 10 with Code Blocks and MingW.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Ducky View Post
    Arrays

    "When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty . In this case, the compiler will assume a size for the array that matches the number of values included between braces

    Sorry i dont get it. This is what i did.
    Nope, that was assignment, not initialization. Initialization is this line:
    Code:
    short n[2] = {0,0};
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty . In this case, the compiler will assume a size for the array that matches the number of values included between braces
    That's correct.

    Quote Originally Posted by Ducky
    Sorry i dont get it. This is what i did.
    No, you attempted to assign to an array, not to initialise an array. You did initialise n, on line 3 of your code snippet.

    Quote Originally Posted by Ducky
    And can i assign only one element of
    Yes, by using the first or second public member variables of the pair, e.g.,
    Code:
    std::pair<short, short> position = GetPosition(c, n);
    std::cout << position.first << ' ' << position.second << std::endl;
    However, you may find it better to define your own Position class instead of using std::pair.
    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

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks to both of you!

    So once an array is initialized you cannot change their elements?
    Using Windows 10 with Code Blocks and MingW.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    So once an array is initialized you cannot change their elements?
    No, you may be able to assign to the elements of an 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

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ah ok i understand. So i should have written something like this i guess.

    Code:
     
    if     (letter == 'a' && number == 1) 
           n[0] = {7} ; 
           n[1] = {3} ;
    Using Windows 10 with Code Blocks and MingW.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    So i should have written something like this i guess.
    More like:
    Code:
    n[0] = 7;
    n[1] = 3;
    but then you would still have the problem where you return a pointer to an object that no longer exists by the time control returns to the caller.
    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

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The thing is that without other changes it would still be wrong to return n (pointer to local array).

    More likely, and with other simplifications:

    Code:
    //pass array in to be modified
    short* GetPosition(short* n, char letter, short number)
    {
        n[0] = n[1] = 0;
        if (letter == 'a' && number >= 1 && number <= 8) {
            n[0] = 7;
            n[1] = 7 * number - 4;
        }
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2003, 11:20 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM