C++,to call array function

Discussion in 'Mac Programming' started by kris8, Apr 20, 2010.

  1. kris8, Apr 20, 2010
    Last edited: Feb 1, 2012
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    If you were given this project, your book or a lecture should have covered how to pass around an array. Assuming a c-style array and not a std::vector, the name of the variable is actually a pointer to the base of the array. So all you need to do is use this name as a parameter to your functions (the type of the parameter will be int *). In the function you can use [] to subscript the array, and can assign to and from positions in the array this way.

    As for how to declare this variable in main... If you have a known max number of items, you can just say:
    Code:
    int list[46];
    if you need to dynamically allocate, you'd declare:
    Code:
    int *list;
    ...
    list = new int[w]; 
    passing around list means passing around apointer to memory that can be updated.

    -Lee
     
  3. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #3
    I agree with Lee; arrays or vectors should have been covered before you got this assignment. Please give us some more detail as to which you're using.

    And I certainly hope it's vector; otherwise you have the bad luck of being stuck in a C course where the instructor thought "Let's use cout instead of printf and call it C++".

    That does not bode well.
     

Share This Page