Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Passing a pointer to an array of pointers to a function :S  (Read 4163 times)

0 Members and 1 Guest are viewing this topic.

Ben

Passing a pointer to an array of pointers to a f...
« on: April 13, 2006, 03:52:26 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 437
I have an array of pointers to objBlocks:
Code: [Select]
objBlock * blocks[3*15];

I want to pass a pointer to that, to a constructor, so that my class can use it, and delete them.
Simple.
But I need to know what the prototype should be. This is my call to the constructor:
Code: [Select]
objBall ball(112,80, &bar, &blocks);
But currently the constructor is:
Code: [Select]
objBall(int x, int y, objBar * bar, objBlock......

Fill in the end :-p.
Pointers with me is a love hate relationship
Logged
Want a place to upload your sprites and games for FREE? Look no further than GameDevotion

aab

^ Evolved from a Hobbit
Re: Passing a pointer to an array of pointers to...
« Reply #1 on: April 13, 2006, 04:08:05 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 992
Code: [Select]
objBall(int x, int y, objBar * bar, objBlock *** blocks )

A *a; means  an A 'is whats at the address of'  a;
A **a; means  an A 'is whats at the address of whats at the address of' a;
and so on.
Can also do things like:
objBall(int x, int y, objBar * bar, objBlock ** blocks[] )

Typedefs can make this more sensilbe reading eg: typedef objBlock* pObjBlock; then have objBall(int x, int y, objBar * bar, pObjBlock&blocks[] ), though then you wouldnt have to pass with '&' infront of the argument (which is preferable).
« Last Edit: April 13, 2006, 04:13:38 pm by aab »
Logged




I ♥ Sol
.... I ♥ Sol ? wtf how long has that been there? >_> *rrrrrrrrar*
  • MySpace

Ben

Re: Passing a pointer to an array of pointers to...
« Reply #2 on: April 13, 2006, 04:33:24 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 437
That yellow is incredibly unreadable XD, I'm on kokiri theme, I like the lighter themes.

Anyway I can manage to get it to accept the data, but not the pointer to it.
If I change the constructor to:
Code: [Select]
objBall(u16 set_x, u8 set_y, objBar * bar, objBlock * blocks[]){
And then pass the data instead of the adress:
Code: [Select]
objBall ball(112,80, &bar, blocks);

It works, but I want to pass the pointer. It's just annoying me.
And I had tried the triple pointer thing before, but that didn't work either :S.
Logged
Want a place to upload your sprites and games for FREE? Look no further than GameDevotion

aab

^ Evolved from a Hobbit
Re: Passing a pointer to an array of pointers to...
« Reply #3 on: April 13, 2006, 05:25:16 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 992
typecast (C-style: not a brilliant thing to do mind you):

objBall ball(112,80, &bar, (int***)(&blocks));

try encapsulating it in a typesdef as well though. It does make things easier. Once you accept the int*** the syntax for reading each int's gonna be crazy.
Ive never actually had a program thats needed deal with that amount of 'pointing', other than experimenting.
Logged




I ♥ Sol
.... I ♥ Sol ? wtf how long has that been there? >_> *rrrrrrrrar*
  • MySpace

Ben

Re: Passing a pointer to an array of pointers to...
« Reply #4 on: April 13, 2006, 05:27:36 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 437
But it's not an int(***) and besides even if that works I still don't know what type I should store it is.
Logged
Want a place to upload your sprites and games for FREE? Look no further than GameDevotion

aab

^ Evolved from a Hobbit
Re: Passing a pointer to an array of pointers to...
« Reply #5 on: April 13, 2006, 05:38:58 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 992
its an int *a[], which is fairly equivelant to an int**, and so the & of it would be an ( int *** ).

When using it, youll have a thing, lets call it a. Take whats at the address of a. *a. this is an int**, or an int*[], so  you would go (*a)[t] to access the int* at t, after which you would go *( (*a)[t] ) to access the int at the t'th int*.

[edit]
I'd recommend finding an alternative to what your doing, but another way to do the same thing:
accept OBJwhatever ** &obj, and pass (OBJwhatever**&)(obj)
If that doesnt work you can typedef OBJwhatever** as something and replace that, so you'd be accepting somethign simple like (ptrptrOBJwhatever & objs)
[/edit]
« Last Edit: April 13, 2006, 05:42:00 pm by aab »
Logged




I ♥ Sol
.... I ♥ Sol ? wtf how long has that been there? >_> *rrrrrrrrar*
  • MySpace
Re: Passing a pointer to an array of pointers to...
« Reply #6 on: April 13, 2006, 06:09:42 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 150
I'm not sure I understand exactly what you're trying to do here, but let me offer a suggestion:

I suppose that you've want a 3x15 array blocks, is this right?

A good idea would be to nix the array. Instead, create a list of pointers[0 to (3*15-1)]. That way, you can just pass the pointer of the first entry in your list to your class.

Since you know that the max x dimension of your array is 15, it would be trivial to process your list. Like so:

Pseudocode:
Code: [Select]
ptr points to the first object in your list.
iTile = 0; MaxTiles = 3 * 15;
iX = 0; iY = 0

while (iTile < MaxTiles)
{
    if !(ptr == 0)
    {
        (object at ptr*).draw(iX, iY)
    }
    iX = iX + 32;
    iTile = iTile++;
    ptr++;
    if (iTile mod 15 == 0)
    {
        iX = 0;
        iY = iY + 32;
    }
}

Note that mod is relatively slow when unoptimized, but is a very easy way of showing how to implement this. As long as your max x dimension is 15 (hexidecimal &h0F), you could do this:

if ((iTile AND &h0F) == 0)
{
...
}

Which would be much faster.
« Last Edit: April 13, 2006, 06:14:05 pm by FarFromHomeFish »
Logged

Ben

Re: Passing a pointer to an array of pointers to...
« Reply #7 on: April 13, 2006, 06:18:15 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 437
I'm now incredibly confused, so I'm gonna do this slowly.

How would I get a pointer pointing to blocks?
Code: [Select]
objBlock * blocks[3*15];
To be honest I'm not entirely sure what blocks actually is, I know it's an array of pointers to objBlock s, but how would you write that in C++?

Sorry, I just need to get this down.
« Last Edit: April 13, 2006, 06:21:44 pm by Ben »
Logged
Want a place to upload your sprites and games for FREE? Look no further than GameDevotion
Re: Passing a pointer to an array of pointers to...
« Reply #8 on: April 13, 2006, 06:31:48 pm »
  • The Broken King
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1259
I don't know if this helps, but if you simply pass in the array, instead of a pointer to the array, it will still edit the actual array. An array just points to the first item of the array anywyas.
Logged
  • Broken Kings [Temp Site]

Ben

Re: Passing a pointer to an array of pointers to...
« Reply #9 on: April 13, 2006, 06:38:03 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 437
If your telling the truth that's fairly helpful :-p
Now would someone please tell me why I can't do this:
Code: [Select]
        objBlock* myBlocks[];
objBall(u16 set_x, u8 set_y, objBar * bar, objBlock* blocks[]){
myBlocks = blocks;
How are they not the same type? :S.
Logged
Want a place to upload your sprites and games for FREE? Look no further than GameDevotion

aab

^ Evolved from a Hobbit
Re: Passing a pointer to an array of pointers to...
« Reply #10 on: April 13, 2006, 06:43:29 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 992
[edit]
You posted while i was posting.
You are expecting it to do what?
You have an array of pointers: So, you would either need an objBlock** that points to the first pointer in the array:
Code: [Select]
objBlock**myBlocks;
objBall(u16 set_x, u8 set_y, objBar * bar, objBlock* blocks[]){
myBlocks = (objBlock**)blocks;
or a reference to equate to it, but they introduce problems into the default implicit operators and copy constructor which would mean certain self definitions and you can be bothered with that right now...your wanting this working before you add anything else lol.
I rarely use [] when passing myself, i just go straight to *'s, though most of the time i avoid them as well...C++ introduced various ways of avoiding or hiding explicit use of pointers.
[/edit]
Consider the following:
Code: [Select]
int array[10];

array[0]=3;
array[1]=65;
array[3]=34545;

int * pints = array;

pints[0]=3;
pints[1]=65;
pints[3]=34545;

*(array+0)=3;
*(array+1)=65;
*(array+2)=34545;

*(pints+0)=3;
*(pints+1)=65;
*(pints+2)=34545;


The same thing is being done four times, just in different ways.

Im gonna go back to the question here:
Quote
I want to pass a pointer to that, to a constructor, so that my class can use it, and delete them.
If thats all you want to do (if you dont want to actually edit the value of the pointers in the array, just the values at the adress of those pointers)
just accept an objBlock**, and pass (objBlock**)blocks.
You can then delete the pointers. However a:what a strange class and b:the array of pointers now passed would be in an invalid state.

« Last Edit: April 13, 2006, 06:48:21 pm by aab »
Logged




I ♥ Sol
.... I ♥ Sol ? wtf how long has that been there? >_> *rrrrrrrrar*
  • MySpace

Ben

Re: Passing a pointer to an array of pointers to...
« Reply #11 on: April 13, 2006, 06:49:06 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 437
aab the thing is that I understand that, but it's the & operator which annoys me :S.
I'm not so sure of it. Because &var is a pointer, but to create a pointer you do:
int * pointer = &var;
And that's just confusing :S.

Also I'm not sure how array brackets fit in with the scheme of pointers. As I said, what exactly is an array of pointers? Is it a pointer to a pointer? :S.
« Last Edit: April 13, 2006, 06:50:43 pm by Ben »
Logged
Want a place to upload your sprites and games for FREE? Look no further than GameDevotion

aab

^ Evolved from a Hobbit
Re: Passing a pointer to an array of pointers to...
« Reply #12 on: April 13, 2006, 07:17:29 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 992
Ok.

Basically, ive written this in this board three times in the past week or maybe a month ago lol..i was on holiday for three weeks so time passed...

Let A be some typename, such that you can declare an object of type A eg: A a,b,c;

Let a be an instance of type A, ie A a;
Let p be an instance of an A*, ie A *p;
Let P be an instance of an A**, ie A **P;

*p means 'whats at the address of' p
&a means 'the address of' a
*a would mean 'whats at the address of' a, but as 'a's not an address thats invalid. (sorry if im going too simple..but uim going to anyway).
&p means 'the address of' p, which is perfectly legitamate, as a pointer is an address stored somewhere in memory: To address a pointer is somewhat unusual, as oto access the data at the very endof the chain of pointers you would need to get the pointer at the address of the first pointer, and then get the variable at the address of it ie **P.

Now, p is not an array.
But if you were to go p[4] = 5; /* assuming an A can be assigned the value 5 */ that would be syntaxically correct.
What is does, is takes the value of p (an A*, ie an address), and uses the A, that is 4 A's along in memory (ie 4*sizeof(A) in bytes ) from the A that p addresses.

When you are using an array, the array variable is actually equivalent in many respects (indeed every explicit respect).
Let array be an array of A, ie: A array[5];

array itself can actually be used just like a pointer to a[0], because, well thats what it is.
if you were to go p = array + 1;, you could go p[0]=5;, and it would be the same as going array[1]=5;
//NOTE: when you add one to a pointer, it increases the physical address which is references by the size of the object it points to, ie p[0] IS *p, p[1] IS *(p+1), p[2] IS *(p+2)

Arrays, being sequential only need to have the address of their first variable stored, as every other element in the arrays position can be calculated from that first address

When looping through an array, it can be more efficient to do , instead of this
Code: [Select]
int array[5];
for( int t=0; t<5 ; ++t )
{
 array[t] = /*..*/
}
to use a pointer, and increment that:
Code: [Select]
int array[5];
for( int*ptr=array; ptr<array+5 ; ++ptr )
{
 *ptr = /*..*/
}
Written more understandably:
Code: [Select]
int array[5];
int*pFirst = &array[0];
int*pLast = &array[4];
for( int*ptr=pFirst; ptr<=pLast ; ++ptr )
{
 *ptr = /*..*/
}

Now, with everything in this topic, thinking hard enough there is no pointer issue you cant resolve.

To figure out what int*p[] would mean, just imagine an int* is another type: alias it in your mind.
Say an int* is actually a type called X.

X p[] would be an array of X's, so p would be a pointer to the first X in the array.
The next X in the array would be the X one X after that in main memory, ie p[1], or *(p+1).

Replacing X with int*, what we have, is a pointer to an int*. There is another int* after that, such that p[1] is another int*, as is p[2].

And as p[0], p[1], p[2] ... OR *(p+1), *(p+2), *(p+3) ... are pointers, you can put '*' infront of them to access the int at their address ie:

*(p[0]) = 5; is legal.

There.
If you have any more Q's, i cant imagine i could do anything more to help other than draw some boxes and label them p[0], p[1], p[2], though judging by people ive worked with before, that actually helps ALOT.



Logged




I ♥ Sol
.... I ♥ Sol ? wtf how long has that been there? >_> *rrrrrrrrar*
  • MySpace

Ben

Re: Passing a pointer to an array of pointers to...
« Reply #13 on: April 13, 2006, 07:41:47 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 437
Yeh that helps a lot, lol.
I knew a lot of that sketchily, it's the most annoying thing about C++, XD.
I'll learn to love it eventually, but I didn't really know about the array thing.
It's astounding how much more simple GM is, XD.

Thanks, aab. I've got it working now anyway.
But for some reason delete myBlocks[address] seems to call the deleting for addresses 0, 1 and whatever address is, it's weird :S. But if i just call the function it calls outright it works, though we'll end up leaking.
Logged
Want a place to upload your sprites and games for FREE? Look no further than GameDevotion
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.222 seconds with 63 queries.

anything