Okay, in my program I have a line of code like this:

if(type == rabbit){pointer = begin.sim[sim].field[x][y].rabbits; begin.sim[sim].field[x][y].rabbits = this;}

In order to see what the issue I was having with the actual program, I put in this line of code:

printf("\n\n%p & %p\n\n", begin.sim[sim].field[x][y].rabbits, this);

And I found that the pointer begin.sim[sim].field[x][y].rabbits was equal to this , when my code was supposed to assign it to the old value of begin.sim[sim].field[x][y].rabbits , it seems to be assigning it to the address of the pointer, not the value of the pointer, so I'm wondering how to make it assign the pointer to the value, not the address, because everything I've tried keeps giving me a syntax error :(

>it seems to be assigning it to the address of the pointer, not the value of the pointer
Yep, seeing as how that's what you told it to do. Unless you do some dereferencing, you can expect to be working only with addresses.

>so I'm wondering how to make it assign the pointer to the value
Let's get one thing straight. You can assign a value to an object, or an address to a pointer. No mixey matchy. If you want to assign the value pointed to by this to the object pointed to by rabbits, you would dereference them both:

*a = *b; // Copy the value pointed to by b to the object pointed to by a

>it seems to be assigning it to the address of the pointer, not the value of the pointer
Yep, seeing as how that's what you told it to do. Unless you do some dereferencing, you can expect to be working only with addresses.

>so I'm wondering how to make it assign the pointer to the value
Let's get one thing straight. You can assign a value to an object, or an address to a pointer. No mixey matchy. If you want to assign the value pointed to by this to the object pointed to by rabbits, you would dereference them both:

*a = *b; // Copy the value pointed to by b to the object pointed to by a

Well, basically I want one to point to the same thing the other one is pointing to already, but I can't figure out the code to do so :/

If you want an alias, that's easy:

int *p = something;
int *q = p;

Now q is an alias for p; they both point to the same thing. However, note that if you change that thing, both p and q will reflect that change. You can reseat either p or q to point somewhere else and that won't affect the other.

I'm getting mixed signals. Perhaps you should explain exactly what you're trying to do rather than how you're trying to do it.

It seems like this is what you're doing now (using object types int and simplified variable names):

int* pointer = 0;

int* rabbit = new int(5);
// rabbit = 0x0134a1a8 and *rabbit=5 (the value at address 0x0134a1a8)

pointer = rabbit;
//  pointer = 0x0134a1a8 and *pointer = 5
// we have still rabbit = 0x0134a1a8 and *rabbit=5

rabbit = new int(8);
// rabbit = 0x013797c0 and *rabbit = 8
// we still have pointer = 0x0134a1a8 and *pointer = 5

if you also want pointer to point to 0x013797c0 , you'll have to add another pointer = rabbit, but then you'll lose reference to object at address 0x0134a1a8 and will leak memory.

If you want to give begin.sim[sim].field[x][y].rabbits a new object at address this, you'll have to delete begin.sim[sim].field[x][y].rabbits and then begin.sim[sim].field[x][y].rabbits = this; But I don't know why you need variable pointer at all then.

Sorry about my previous posts, I was in a hurry, I can explain it more fully now.

My program is a nature simulation, in it I have a 30x30 array of a pointer class, which contains pointers to enumerations of type animals , one of these pointers is a pointer to the rabbits in that specific spot. The animals class is this:

class animals
{
	public:
		int food;
		int x, y;
		int sim; bool ran;
		int repro;
		animaltype type;
		animals * back;
		animals * next;
		void actions(int hour);
		int findtotal(animaltype whichone);
		animals(animaltype classification);
		animals * get(animaltype classification);
		void move(int x, int y);
		~animals();
};

Yes, I know that it's a bit ridiculous to have everything in public access, I intend to move it into private when I fix this problem. Anyways, the constructor for this class is:

animals::animals(animaltype classification)
{
	type = classification;
	if(type == moose){food = 300;}
	if(type == wolf){food = 100;}
	if(type == rabbit){food = 20;}
	if(type == grass){food = 0;}
	repro = 0; ran = true;
	back = NULL;
	next = NULL;
}

And I have a create function that runs this section for rabbits:

if(classification == rabbit)
{
	pointer = field[x_coord][y_coord].rabbits;
	if(pointer == NULL){field[x_coord][y_coord].rabbits = new animals(rabbit); pointer = field[x_coord][y_coord].rabbits; pointer->sim = simnumber; pointer->x = x_coord; pointer->y = y_coord;}
	else
	{			
		while(amount > 0)
		{
			field[x_coord][y_coord].rabbits = new animals(rabbit); pointer->back = field[x_coord][y_coord].rabbits; pointer->back->next = pointer; pointer = pointer->back; pointer->sim = simnumber; pointer->x = x_coord; pointer->y = y_coord; amount--;
		}
	}
}

I apologize for the tight code, so at the beginning of the program, 2 rabbits are created in each linked list in all 900 sections of the array, using that function.

Then, each hour, every animal is run through, and this is the code that runs for each section of the array:

pointer = field[x][y].rabbits;
while(pointer != NULL)
{if(!pointer->ran){pointer->actions(hour); pointer = field[x][y].rabbits;}else pointer = pointer->next;}

And before that, I also run code for each X and Y coordinate, in order to reset all the ran bools, here that is:

pointer = field[x][y].rabbits;
while(pointer != NULL){pointer->ran = false; pointer = pointer->next;}

And currently I am only testing the movement function, so all the actions function does is this:

ran = true;
if(type == rabbit)
{
	if(hour % 8 == 6){move(x+random(-1, 1), y+random(-1, 1));}
}

And finally, here's how the move function looks(only showing the rabbit parts):

void animals::move(int x_coord, int y_coord)
{
	if((x_coord >= 0 && y_coord >= 0 && x_coord <= 29 && y_coord <= 29) && (x_coord != x && y_coord != y))
	{
		if(back != NULL){back->next = next;}
		else
		{
			if(type == rabbit){begin.sim[sim].field[x][y].rabbits = next;}
		}
		if(next != NULL){next->back = back;}
		x = x_coord;
		y = y_coord;
		animals * pointer;
		if(type == rabbit){pointer = begin.sim[sim].field[x][y].rabbits; if(pointer == NULL){begin.sim[sim].field[x][y].rabbits = this;}}

		if(pointer != NULL)
		{
			if(type == rabbit){begin.sim[sim].field[x][y].rabbits = this; next = pointer; back = NULL; pointer->back = this;}
		}
	}
}

So, this code successfully runs the move function once, however, when it runs it for the second time, the program freezes and from what I can tell it gets stuck in the code that is supposed to run through the linked list, and what ends up happening is I get a rabbit with its next pointer pointing to the start of its linked list, so it just keeps going back and forth, and so I'm almost certain that my issue is in this line:

if(pointer != NULL)
{
	if(type == rabbit){begin.sim[sim].field[x][y].rabbits = this; next = pointer; back = NULL; pointer->back = this;}
}

Because if I comment that out, the program no longer freezes, so I think the issue is that when I assigned pointer to begin.sim[sim].field[x][y].rabbits the first time, it assigned it to the address of the pointer, not the address that the pointer was pointing to.

Basically, the move function should take the rabbit from where it was, move it to the front of the linked list in its new X-Y coordinate, however, I cannot figure out how to reference to the address a pointer is pointing to, and not the address of the pointer itself when assigning another pointer to it.

If you want the address of the pointer you would want to say

pointer = &begin.sim[sim].field[x][y].rabbits;

that will give you the address of the pointer not the address that the pointer holds. hope thats what you wanted

If you want the address of the pointer you would want to say

pointer = &begin.sim[sim].field[x][y].rabbits;

that will give you the address of the pointer not the address that the pointer holds. hope thats what you wanted

Well, it currently looks like that is what I'm getting, and if I try to put an referencing operator in front, like you showed, my compiler gives a syntax error( I am using MS Visual C++ 6.0), however, this is NOT what I want, I want to assign it to the address that the other pointer is HOLDING, not the address of the pointer itself.

could you please post the declaration of "pointer"

It's animals * pointer; in every declaration of it.

Thank you. okay to make sure you want pointer to hold the rabbit object right?

Yes, I want to assign the pointer to the address held by begin.sim[sim].field[x][y].rabbits , because I then later change what that pointer points to, so it points to the moving rabbit, and I still want a way to access what it was previously pointed to by it. To my knowledge I'm doing it right, but from my tests it seems I am not :(

alright. what is going on here i believe is that begin.sim[sim].field[x][y].rabbits is giving you a pointer of a pointer so to get down to the original pointer try *begin.sim[sim].field[x][y].rabbits i hope that this will work

I've already tried it and I get this syntax error:
C:\<file path name here>(612) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class animals' (or there is no accept

I assume it's because it's trying to assign the pointer to a value of a class, and since I don't have an overloaded operator for the = sign of animals, and it's a pointer, not another animal object, it can't convert it over.

would you mind terribly much if you messaged me with the code attached so i could try something?

Hm, upon retesting I may have been wrong, it seems that it is being assigned properly :( I apologize for leading in the wrong direction.

Nonetheless, I still have an issue with it looping, and the reason is because somehow on the first rabbit in the linked list, their next pointer gets assigned to the head of the linked list, field[x][y].rabbits , so it just loops back and forth between one and the other, and I don't know where that pointer would get assigned to the head of the linked list, I think it may be when back is assigned, but I'm not certain..

well now you have another beast to deal with. the best of luck getting it running.

well when i run the simulation it displays hex numbers for awhile and then it will show me 2 sets of hex numbers and then ask me to press enter to continue and every time i do it just does it again. not quite sure whats going on yet but ill get back to you tomorrow afternoon/evening with anything that i find.

Oh, sorry, that was a debugging code I added in, there's two printf s in the sim function, removing both of those should put it back to normal.

Ah ha! After 5 days of pouring over my program I found it! And it was a very silly mistake too :P

In the Move function, while I assigned the next and back pointers properly if the pointer != NULL, I never changed both to NULL if it DID equal NULL, and that was my problem, so this is solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.