Jump to content
  • Advertisement
Sign in to follow this  
Avont29

help with pointers

This topic is 4847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, could someone please help me with pointers, im just not getting it ok, is this right? // more pointers #include <iostream.h> int main () { int value1 = 5, value2 = 15; int *p1, *p2; p1 = &value1; // p1 = address of value1 p2 = &value2; // p2 = address of value2 *p1 = 10; // value pointed by p1 = 10 *p2 = *p1; // value pointed by p2 = value pointed by p1 p1 = p2; // p1 = p2 (value of pointer copied) *p1 = 20; // value pointed by p1 = 20 cout << "value1==" << value1 << "/ value2==" << value2; return 0; } declare and initialize 2 variables, value1 and value 2. p1, and p2, are pointers that point to an integer p1 = the address of value 1 p2 = the address of value 2 ok, im lost after that

Share this post


Link to post
Share on other sites
Advertisement
1. int number = 10;
2. int *p;

3. p = &number;

number is an integer with the value 10. This means that somewhere in the memory pool there is a memory cell which hold the value 10.
you refer to this value by simply using the declared integer named "number"

Now, p is a pointer to an int type.
when specifing p = &number , p now holds the memory address of the memory cell which holds the value of "number". The memory address is also a number, but is its NOT the value pointed by p. The value pointed by p is the value STORED at the memory address which p holds.
In order to refer to the value pointed by p, the '*' character is used.
thus, *p refers to the value STORED at the memory address that p holds.

If we continue the code above and do :
*p = 20;

The value which is pointed by p will be changed from 10 to 20.
And because the value of number is also stored at that memory addres, its value
also will be 20.

Hope that helped ;)

Share this post


Link to post
Share on other sites
*p1 = 10; // value pointed by p1 = 10

you store 10 to where p1 is pointing to (in this case value1).

*p2 = *p1; // value pointed by p2 = value pointed by p1

get the value of where p1 (value1) is pointing to and set that value to where p2 is pointing (value2)

p1 = p2; // p1 = p2 (value of pointer copied)

p1 is now pointing to the same address as p2 (p1 & p2 is now pointing to value2)

*p1 = 20; // value pointed by p1 = 20

set 20 to where p1 is pointing to, this will cause *p2 to be 20 as well

cout << "value1==" << value1 << "/ value2==" << value2;

output should be 10, 20 for value1 and value2 respectively

Share this post


Link to post
Share on other sites
Okay, the deal with pointers are, they're really easy to get confused by newbies, and I understand, as it took me a day or so to really get them down.

Now, with the code you've shown, here's what's happening (look at the comments);


// more pointers
#include <iostream.h>

int main ()
{
int value1 = 5, value2 = 15; // declaring two variables, and inititalizing
int *p1, *p2; // declaring two pointers to integers

p1 = &value1; // the VALUE of the pointer p1 is the memory address of value1
p2 = &value2; // the VALUE of the pointer p2 is the memory address of value2
*p1 = 10; // The value in the memory address the pointer is storing is
// changed to 10. value1 is now equal to 10
*p2 = *p1; // the value at the memory address pointed by p1 is assigned
// to p2. value2 value is now equal to value1
p1 = p2; // the memory addresses that the pointers point to are now
// equal
*p1 = 20; // value in the variable at the memory address pointed to by the
// pointer is 20
cout << "value1==" << value1 << "/ value2==" << value2;
return 0;
}




I hope this helped, if you have any more questions, or need further explanation of pointers, you can PM me, I don't mind helping you out!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!