CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2002
    Posts
    63

    Indexing an array a[] with a+1

    Maybe you can help me with a small question. It is kind of hard to explain, but I made a little code example that I hope explains my thoughts.

    I'm thinking of the code that calls the print function with the second element in the array: print(a+1);

    What I think I'm doing is that I sends an pointer to a[1] into the function print.

    It seems to works as I intended but is it safe to do it this way? (Nevermind that it is not the simplest or prefered way to do this.) Also, if the class Something is an subclass to another class, does it change anything?

    Code:
    #include <iostream>
    using namespace std;
    
    class Something
    {
    public:
    	Something(int in = 0){ n = in; };
    	void setData(int in) { n = in; };
    	int  getData() { return n; };
    protected:
            int n;            
    };
    
    void print(Something *in)
    { 
        cout << in->getData() << endl; 
    }
    
    int main()
    {
        Something a[5];
        a[0].setData(1); 
        a[1].setData(2); 
        a[2].setData(3); 
        
        print(a+1);
        
        char wait;
        cin.get(wait);   
    }

  2. #2
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Indexing an array a[] with a+1

    Quote Originally Posted by Suzi
    What I think I'm doing is that I sends an pointer to a[1] into the function print.
    Yes, that is correct and it is safe.

    a+1 is the same as &a[1].

    My hobby projects:
    www.rclsoftware.org.uk

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Indexing an array a[] with a+1

    Maybe it's just me but I feel that &a[1] is less obscure though so in this context I would have used that.

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Indexing an array a[] with a+1

    Better:
    Code:
    void print(Something& in)
    { 
        cout << in.getData() << endl; 
    }
    
    int main()
    {
        Something a[5];
        a[0].setData(1); 
        a[1].setData(2); 
        a[2].setData(3); 
        
        print(a[1]);
    }
    Even better:
    Code:
    class Something
    {
    public:
    	Something(int in = 0){ n = in; };
    	void setData(int in) { n = in; };
    	int  getData() const{ return n; };
    protected:
            int n;            
    };
    
    void print(const Something& in)
    { 
        cout << in.getData() << endl; 
    }
    If you are not intending to pass NULL (and you are not checking for it inside the function), use references, not pointers. And learn about const correctness ;-)
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Indexing an array a[] with a+1

    Might want to overload the ostream& operator<<(ostream&, const Something&) so that you could directly do:
    Code:
    std::cout << *(a+1);
    //or
    std::cout << a[1];

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Indexing an array a[] with a+1

    Quote Originally Posted by S_M_A
    Maybe it's just me but I feel that &a[1] is less obscure though so in this context I would have used that.
    nope. it's not just you. I agree. print(a+1) is vague. It's not obvious that you are passing a pointer to an object just by looking at the code. In a really large program that would be a bad coding technique and would result in software that is difficult to read and maintain.

    The suggestion to overload stream operator was really good as well as treuss' suggestion to use references rather than pointers. Pointers can be a pain because they could be set to NULL. If you pass a lot of pointers to things through functions, then you have to validate them somehow so that you don't end up with undefined run time behavior. The function taking a pointer has no idea if the caller set the value correctly.

  7. #7
    Join Date
    May 2002
    Posts
    63

    Re: Indexing an array a[] with a+1

    Hey,
    thanks all for your answers!

    Lots of great suggestions, I know that my example isn´t the best but I only made it to demonstrate my question. It´s not something I would use in a real project but I´m really thankful for all your help and ideas to make my code better

    /Suzi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured