Click to See Complete Forum and Search --> : Indexing an array a[] with a+1


Suzi
February 22nd, 2008, 03:36 AM
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?


#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);
}

Zaccheus
February 22nd, 2008, 03:44 AM
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].

:)

S_M_A
February 22nd, 2008, 04:13 AM
Maybe it's just me but I feel that &a[1] is less obscure though so in this context I would have used that.

treuss
February 22nd, 2008, 04:56 AM
Better: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: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 ;-)

exterminator
February 22nd, 2008, 02:03 PM
Might want to overload the ostream& operator<<(ostream&, const Something&) so that you could directly do:

std::cout << *(a+1);
//or
std::cout << a[1];

kempofighter
February 22nd, 2008, 05:52 PM
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.

Suzi
February 25th, 2008, 02:02 AM
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