4 Replies - 371 Views - Last Post: 30 December 2011 - 04:10 AM Rate Topic: -----

#1 Bennos2cool4u  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-July 11

Function Parameter: Pointer to Array of Classes

Posted 30 December 2011 - 03:32 AM

Okay, I'm back with more random question.

How does one, if one does, handle an array of class identities with a pointer. Further more, how does one pass this to a function as a parameter.

I'd assuming that for the array I have to initialise the pointer, pass the data and then handle the new pointer with the function.

This is not what I'm looking to do however, as I simply want to pass the address of the array of classes to the function, and handle it's contents with a local pointer.

If this doesn't make sense, I cannot access the data of "Relatives" properly in the following code:

void Instance::Update(bool keyState[246], bool nodes[10][23], Instance (*Relatives)[250], int *pNextInstance)
{
	if (type == Block)
	{
		if(fallTick < 15) 
			fallTick++; 

		else 
		{
			for(int i = 0; i < 3; i++)
			{
				if (nodes[(int)Relatives[i]->x / 16][(int)Relatives[i]->y / 16 + 1] == false || y < 0)
					{vObstruct = false;	continue;}
				else
				{
					ShowDebugMessages("Landed\n");
					for (int j = 0; j < 4; j++)
					{
						int sizeArray[10][2]; 
						sizeArray[sprite][1] = width;
						sizeArray[sprite][2] = height;

						*Relatives[*pNextInstance] = Instance(1, sprite, 0, 0, Relatives[j]->x, Relatives[j]->y, sizeArray, false); 
						nodes[(int)Relatives[j]->x / 16][(int)Relatives[j]->y / 16] = true;
						Relatives[j]->y -= 160;
						*pNextInstance++;
					}
					vObstruct = true;
					break;
				}

			}

			if (vObstruct == false)
			{
				Relatives[0]->y += 16;
				Relatives[1]->y += 16;
				Relatives[2]->y += 16;
				Relatives[3]->y += 16;
				
				fallTick = 0;
			}
		}

	if(shiftTick < 4) 
		shiftTick++; 
		
		

		return;
	}
}


Call to the function is as follows where Object is an 1D Array, size 250, of classes "Instance"
Object[1].Update(keyState, gridNodes, &Object, &nextInstance);


Regards,
Benson

If need be, I can repost the code commented out to show the workings of particular lines.

Is This A Good Question/Topic? 0
  • +

Replies To: Function Parameter: Pointer to Array of Classes

#2 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: Function Parameter: Pointer to Array of Classes

Posted 30 December 2011 - 03:53 AM

Relatives is a pointer to an array, but you sometimes handle it like an array of pointers. Incorrect:
Relatives[i]->x

Correct:
*Relatives[i].x
//or you might have to do it like this
(*Relatives)[i].x


If you pass an array to a function, it automatically decays to a pointer, so you can declare Relatives as:
Instance Relatives[250]

Then use it like this:
Relatives[i].x

And call the function like this:
Object[1].Update(keyState, gridNodes, Object, &nextInstance);

Was This Post Helpful? 0
  • +
  • -

#3 Bennos2cool4u  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-July 11

Re: Function Parameter: Pointer to Array of Classes

Posted 30 December 2011 - 03:59 AM

Question is, although arrays are defined as pointers, will passing array to array as per your second suggestion allow altering of the original array? (Sorry, I should have mentioned that. Though I'm in the process of trying the first method which I attempted before posting this, but I don't think I used the right syntax)
Was This Post Helpful? 0
  • +
  • -

#4 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: Function Parameter: Pointer to Array of Classes

Posted 30 December 2011 - 04:04 AM

Yes, you can still modify the array members.
Was This Post Helpful? 0
  • +
  • -

#5 Bennos2cool4u  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-July 11

Re: Function Parameter: Pointer to Array of Classes

Posted 30 December 2011 - 04:10 AM

View PostHezekiah, on 30 December 2011 - 04:04 AM, said:

Yes, you can still modify the array members.


Never mind, first option worked a charm anyway, like I said, it was my syntax. I Ctrl + Z'd and saw that I had the array index enclosed in the brackets.

Thanks for your help, now to debug the rest.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1