Thread: Accessing Arrays

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    Accessing Arrays

    Hi

    I have functions within a .ccp file named "menus.cpp", which access an array, which is defined at the top of this .cpp file.
    All the functions work fine at the moment, but I need to split this big "menus.cpp" file up into smaller classes.

    If I move the array to the top of the "main.cpp" file where I will call the functions from the "menus.cpp" file - how do I allow different classes to access the array in the "main.cpp" file...?

    Cheers

    PK

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about passing the array as a parameter rather than relying on some global variable kludge?

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or else just use extern.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    4


    I hate to do this to your guys n gals, but...
    ... could you please explain further please?
    (Both Salem's and dwks's methods... I am googling both after submitting this )

    CHeers

    PK

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can declare a global variable in one file and declare it in another the same way except with extern at the beginning. Or you can pass the variable to the functions that need it, as Salem suggested, which is a much better idea.

    What needs more explaining? Okay, here's an example.
    Code:
    /* main.c */
    #include <iostream>
    
    int two;
    
    void what(int two);
    
    int main(void) {
        int one;
    
        one = 3, two = 6561;
    
        return 0;
    }
    Code:
    /* other.c */
    #include <iostream>
    
    extern int one;
    
    void what(int two) {
        std::cout << "one = " << one << "\ntwo = " << two << std::endl;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I hate to do this to your guys n gals, but...
    ... could you please explain further please?
    Code:
    #include <iostream>
    using namespace std;
          
    void someFunc(int data[], int size)  //can be in a separate .cpp file
    {
        for(int i=0; i<size; i++)
        {
            cout<<data[i]<<endl;
        }
    }
    
    int main()
    {
        int numbers[] = {10, 20, 30};
        someFunc(numbers, 3);
        
            
        system("PAUSE");
        return 0;
    }
    If you use a <vector> instead of an array, you won't have to pass the size around:
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
          
    void someFunc(vector<int> data)  
    {
        for(int i=0; i<data.size(); i++)
        {
            cout<<data[i]<<endl;
        }
    }
    
    int main()
    {
        vector<int> numbers;
        numbers.push_back(10);
        numbers.push_back(20);
        numbers.push_back(30);
        
        someFunc(numbers);
        
                
        system("PAUSE");
        return 0;
    }
    Last edited by 7stud; 12-30-2005 at 05:27 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Don't you need <cstdlib> for system()?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You SHOULD need it, but some compilers will let you get away with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 11:20 AM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. accessing arrays with pointers
    By Nutka in forum C Programming
    Replies: 9
    Last Post: 09-30-2002, 08:19 PM