How to use classes in function using pointers?

I have this code
class here
{
public:
int h;
int b;
};
---> main program <----
here array[10];
lol(&array);

void lol (here *array);
{
array->h
}

this code points only to the first memory blocks of the array like [0]. how do i do it that i can use other ones lke [1],[2],[3] if there was data in them?
You would have to use pointer arithmetics to increase the pointer pointing to the array.
Reference: http://cplusplus.com/doc/tutorial/pointers/
Looked at them and still no idea how to do it cuz if i say something like (array)++ it will only give the the [1] value and not all the values in the array
This is simpler than it seems.

Arrays without their [brackets] can be cast to a pointer to the first element.

Furthermore, using [brackets] on a pointer treats the pointer as if it were an array. Therefore for the most part arrays and pointers can be used the same way:

1
2
3
4
5
6
7
8
9
10
11
12
13
void lol(here* array)
{
  array[0].h = 0;  // access [0] element
  array[1].h = 1;  // access [1] element
}

//...

int main()
{
  here array[10];
  lol(array);  // no need for & operator
}
Please disregard the previous content.

A better way to do this would be:

1
2
3
here array[10];
for(int i=0; i<10; i++)
	lol(array[i]);
Last edited on
Topic archived. No new replies allowed.