Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Trying to pass an array

Trying to pass an array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ericelysia
    wrote on last edited by
    #1

    I am having some difficulty passing an array. If someone can shed some light here, I would appreciate it greatly.

    to testCaseArray1[ ] int guessCount1 = 0; char choice1 = myChar; bool correctAnswer1 = myBool;

    C N 2 Replies Last reply
    0
    • E ericelysia

      I am having some difficulty passing an array. If someone can shed some light here, I would appreciate it greatly.

      to testCaseArray1[ ] int guessCount1 = 0; char choice1 = myChar; bool correctAnswer1 = myBool;

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      You should declare your function this way:

      void testFunction(int, int, bool, int, int, int*, char);

      There is another thing wrong in your program. This line:

      to testCaseArray1[ ]

      You cannot declare an array this way (arraySize1 is not a constant). You need to dynamically allocate your array:

      int* testCaseArray1 = new int[arraySize1];

      And don't forget to delete it when you don't need it anymore. I would suggest that you take a look at container classes from the STL. It might be hard to understand for a beginner but it solves a lot of problems (memory management and so on).


      Cédric Moonen Software developer
      Charting control [v1.1]

      1 Reply Last reply
      0
      • E ericelysia

        I am having some difficulty passing an array. If someone can shed some light here, I would appreciate it greatly.

        to testCaseArray1[ ] int guessCount1 = 0; char choice1 = myChar; bool correctAnswer1 = myBool;

        N Offline
        N Offline
        Newbie00
        wrote on last edited by
        #3

        OK, but where is your question? :O

        ericelysia wrote:

        int testCaseArray1[ arraySize1 ];

        That won't work because you have to initialize your array with constant expressions. So the array should be defined like this: int testCaseArray1[5]; If you want to initialize different size arrays you have to use new operand with pointer: int mySize; mySize=5; int *testCaseArray1=new int[mySize];. When the array initialized with new operand is no longer needed you have to delete it with delete operand: delete [] testCaseArray1;

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups