How to return two elements of an array

This is a discussion on How to return two elements of an array within the C++ Programming forums, part of the General Programming Boards category; I would like to return both elements of n[]. I cant find out how to do it. Code: short* GetPosition(char ...

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

    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    That doesn't look like valid code.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    795
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    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);
        // ...
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    795
    Thank you Laserlight!

    Code:
    n[] = {7,  3};
    May i ask why would this be invalid? Im assigning values to an array.
    Compiler MSVC++ 2013 with Code::Blocks.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    795
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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
    21,720
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    So once an array is initialized you cannot change their elements?
    Compiler MSVC++ 2013 with Code::Blocks.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    795
    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} ;
    Compiler MSVC++ 2013 with Code::Blocks.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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).

Page 1 of 2 12 LastLast
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, 06: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, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21