locked
How do I send an array literal to a function RRS feed

  • Question

  • Hey guys, I am really stuck on this. I want to send an array literal to a function I have but I simply cannot suss out how it is done.

    Example

    #include <iostream>
    
    int add ( int myArray [])
    {
           int temp1 = myArray[0];
           int temp2 = myArray[1];
    
          return temp1 + temp2;
    }
    
    int main()
    {
         std::cout << add({2,4});
    
    
    
         return 0;
    }
    I have tried add([2,5]); and add((2,6)); but nothing seems to work is it even possible to do?

    Thank you in advance for any help on this :)

    Steve
    Monday, June 22, 2009 4:35 PM

Answers

  • The C++ language has no concept of an array literal.  The closest you could get is by using a string:

      add("2,4");

    Parsing that is not cheap of course.

    Hans Passant.
    • Marked as answer by StevenBarnes Monday, June 22, 2009 6:13 PM
    Monday, June 22, 2009 5:36 PM

All replies


  • int main()
    {
        int arry[2] = {2,4};
        std::cout << add(arry);

         return 0;
    }

    - Wayne
    Monday, June 22, 2009 4:50 PM
  • Thank you for your answer Wayne but thats not really using an array literal. You have used a literal to initilize arry and then used arry as a parameter. What I am looking for is a way to cut out the array declaration.

    For instance

    int main()
    {
         string str = "hello";
         cout << str;

         //can be done like this instead
         cout << "hello";
    }
    Monday, June 22, 2009 4:59 PM
  • Quote>What I am looking for is a way to cut out the array declaration.

    Why? What is your actual requirement?

    Alternatives include:

    Change your function to take two ints.

    (Or two pointers to ints, or two references to ints.)

    int add (int myArray0, int myArray1)
    {
        return myArray0 + myArray1;
    }

    Then use it like this:

    std::cout << add(2,4);

    - Wayne
    Monday, June 22, 2009 5:14 PM
  • You're trying to pass an initializer list without an array being created.
    You can't initialize something which doesn't exist.

    Initializers have to be supplied at object creation. Afterwards you must use
    assignments or copy operations to set the elements of an existing array.

    - Wayne
    Monday, June 22, 2009 5:23 PM
  • The C++ language has no concept of an array literal.  The closest you could get is by using a string:

      add("2,4");

    Parsing that is not cheap of course.

    Hans Passant.
    • Marked as answer by StevenBarnes Monday, June 22, 2009 6:13 PM
    Monday, June 22, 2009 5:36 PM
  • Ok thank you once again for your answers Wayne. Let me try and explain what am am trying to do a bit more.

    I have a class called Card below is the constructor for it.

    Card(string cardName, string cardType, string colour,

    int cardCost[], int manaAmount[], bool tapped, int powerAndToughness[]);

    This allows me to make cards like so

    int cardCostArray = {0,0,0,0,0,0,0}; //costs nothing to lay
    int manaArray = {1,0,0,0,0,0}; // produces 1 green mana
    int pAndTArray = {0,0,1}; // not a creature creature
    Card land("Forest","basic land","Green",cardCostArray,manaArray,false,pAndTArray);

    As you can see the only time I will use these arrays is when I create an object of type Card but every object I create will be different so instead of having to write 4 lines of code like the previous example I would like to know of a way to create a card object like this:-

    Card land("Forest","basic land","Green",{0,0,0,0,0,0},{1,0,0,0,0,0},false,{0,0,1});

    Hope this helps to clear up a bit more what I am trying to do.

    Thanks

    Steve

    Monday, June 22, 2009 5:51 PM
  • Thank you for your answer nobugz (if only that were always true lol) that is what I needed to know if it was possible or not. Also thank you to Wayne for also taking the time to try and help me.

    Steve
    Monday, June 22, 2009 6:12 PM
  • Quote>int cardCostArray = {0,0,0,0,0,0,0}; //costs nothing to lay
    Quote>int manaArray = {1,0,0,0,0,0}; // produces 1 green mana
    Quote>int pAndTArray = {0,0,1}; // not a creature creature

    These would never compile.

    Don't use a raw array of ints. Define your own array classes (or structs).
    Use them where needed. For example:

    #include <iostream>

    class myArrayClass {
        public:
            int myArray[2];
            myArrayClass(int x, int y) {myArray[0] = x; myArray[1] = y;};
    };

    int add (myArrayClass& mAC)
    {
        return mAC.myArray[0] + mAC.myArray[1];
    }

    int main()
    {
         std::cout << add(myArrayClass(2,4));
         return 0;
    }

    - Wayne
    • Edited by WayneAKing Monday, June 22, 2009 7:50 PM remove unneeded include
    Monday, June 22, 2009 7:06 PM
  • sorry I forgot the []

    //Array to hold cost

     

    int freeToLay[7] = {0,0,0,0,0,0,0};

     

     

    //Array to hold mana produce

     

    int mana[7] = {1,0,0,0,0,0,1};

     

    //Array to hold not a creature

     

    int notACreature[3] = {0,0,0};

    Card land(

    "Forest","Land","Green",freeToLay,mana,false,notACreature);

    This does compile btw.

    I just copy and pasted your code into msvs pro and I like what that does although I don't fully understand how it works yet.

    Monday, June 22, 2009 7:44 PM