Pass array of pointers to a function

Pages: 12
Ehh... Go to reference page, read about pointers.

The problem is, you messed 2 methods up.

Function prototype says:
 
int RentCharge(int, int, int, TileData*);


Which means:

I want you to pass me following arguments:
-integer( x3)
- Pointer to TileData.

Now, what you are doing, is:
Passing 3 integers
Passing TileData(seriousely)

Asterisk(*) is dereference operator.
When dealing with pointers, you must understand them, and maybe find a way to make it easier. Let's look at following example:

1
2
3
int* x;
int y = 8;
x = &y;


Now, what is *x, x, and &x?
I declare data with asterisk near data type(e.g. int* ) because then I can use my method of taking asterisk from the type name to value name. It may be easier for you, it doesn't have to. It's my method.

So, if you have int* x, then you have x alone. x is of int* type.

Now, if you take * from int* and give it to x, you have *x - and *x is of int type.


Accordingly, if you had char*** something, then if you did **something, it would be of type char*. It's quite simple, imho.

So again:
*x is what x is pointing to(in my example - int)
x is adress of what x is pointing to(and it's of int* type)
&x is adress of x(yeah, every value has its own adress). You can use it to create pointer-to-pointer.

Therefore :

1
2
3
4
5
6
7
int RentCharge(int,int,int,TileData*);//Prototype

TileData* pTileData = new pTileData; // idk pTileData, I assume that it was initialized correctly

rent = RentCharge(tile, owner, move, *pTileData);//Wrong! If we take * from TileData*, we have type of TileData - and program wanted us to give TileData*.

rent = RentCharge(tile, owner, move, pTileData); // Correct - asterisk stays with TileData*, and type provided is ok. 

Pointers ain't no easy, so brief explanation from me. If you want more -

http://www.cplusplus.com/doc/tutorial/pointers/

Cheers!
Thanks for your explanation, previous posts claimed that the position of the * did not matter, a programmers syntax style where both are legal. Are you saying though that if you choose one style, (i.e. int* p or int *p) that affects the type of the pointer? - I've certainly not come across that in any tutorial.

Thanks for the tutorial link, that is slightly better than others i've read but none of them go into detail on how to pass pointers between functions - which is (clearly) where I am struggling.

I do understand what you are saying though, I will make the changes and give it a go.

Thanks for your patience

No, the spacing of the pointer makes no difference! You can even have no spacing at all and it means the same thing. Spacing doesn't affect anything in C++.
Topic archived. No new replies allowed.
Pages: 12