Pointers in c++

Want to know all hings about pointers

Page 1 of 1

6 Replies - 805 Views - Last Post: 21 December 2010 - 11:37 AM Rate Topic: -----

#1 MAYANK7789  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-July 09

Pointers in c++

Posted 19 December 2010 - 10:31 AM

Hello everyone,

Can anybody give me the complete tutorial of c++ pointers?
I will be very thankful to you if some one gives me some tutorial about pointers...
Is This A Good Question/Topic? 0
  • +

Replies To: Pointers in c++

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,781
  • Joined: 27-December 08

Re: Pointers in c++

Posted 19 December 2010 - 10:39 AM

Moved to C++ Help from the Tutorials Section.

Since you posted this in the C++ Tutorials Section, I would start looking there. I know there are quite a few good pointers tutorials there. :)
Was This Post Helpful? 2
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Pointers in c++

Posted 19 December 2010 - 04:28 PM

There are lots of good tutorials on pointers (google), but one thing I will say is that pointers are alot of more simple than they seem at first. To start with, know that pointers are just like regular variables, except they hold addresses (references) to data, instead of the immediate data itself. For example.

int a = 10;

// "ptr = the address of a" ptr now holds the address of the variable "a", so you can use "ptr" to access the data stored at that address.
int *ptr = &a;

// to dereference a pointer, use the astrix, this means that you are going to the address stored in "ptr" and changing 4 bytes (an integer) to 100.
*ptr = 100;

Was This Post Helpful? 0
  • +
  • -

#4 Slumdog  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 116
  • Joined: 26-November 10

Re: Pointers in c++

Posted 19 December 2010 - 09:34 PM

You will understand what pointers are once you work with them. Create a small application with bunch of pointers pointing to a variable. Print the variables' addresses and print what the pointers' are pointing to. Just keep messing with 'em.

I assume you are having trouble understand pointers because you don't seem to understand addresses.

Look at this:
0x13FF60 
0x13FF64
0x13FF68
...
...
...



They are addresses; they contain a certain value. Addresses are, essentially, a "bookmark"/"address"(hence the name) in the memory where data is stored. They can be any datatype. In this case, we will work with integers. Imagine addresses are like our house or house numbers and the value the address holds is the number of people who live in the house.

Consider this:
int numOfPeopleInTheHouse = 10;
std::cout << &numOfPeopleInTheHouse; //assume 0x13FF60



That piece of code should output the address of the variable numOfPeopleInTheHouse; the address of the variable 'numOfPeopleInTheHouse', in this case, is 0x13FF60. The value that address holds 10. Lets create a pointer.

int x = 10;
std::cout << &num << std::endl;
int *p; // you can even do int *p = &num.
p = &num;
std::cout << *p << std::endl; //prints 10
*p = 9001; //the value of x is changed.
std::cout << x << std::endl; //surprise.



We created a pointer; the variable 'p' holds the address of 'x'(0x13FF60). If we dereference it using the asterisks sign, we can get the value of the address. If you do this: *p = 9001. This is, essentially, changing the value the pointer is pointing to. The value of 'x' would be 9001.

Hope that helps. Peace.

This post has been edited by Slumdog: 19 December 2010 - 09:36 PM

Was This Post Helpful? 1
  • +
  • -

#5 Guest_kosta*


Reputation:

Re: Pointers in c++

Posted 20 December 2010 - 03:43 AM

View Postkosta, on 20 December 2010 - 02:31 AM, said:

View PostMAYANK7789, on 19 December 2010 - 09:31 AM, said:

Hello everyone,

Can anybody give me the complete tutorial of c++ pointers?
I will be very thankful to you if some one gives me some tutorial about pointers...


I dont know if that will help you but it helped me undestand pointers:

First of all consider this:

int x = 10;
int *ptr = &x //This will be your declaration

Now:
when you have the "ptr" or "&x" you know that it holds the ADDRESS of the variable 'x'.
look 'ptr' or '&x' as a CLOSED BOX.
All you know with the 'ptr' is where this box is located in the RAM. you DO NOT know what value it has inside this box because it is CLOSED!
But there is a 'key' that will make you open this box and see whats inside(what value). This key is the '*' derefernce operator.. Now that you have that key and opened the box, you can see that inside there is the value of 10 (ex. cout << *ptr; //10 )
... Since the box is open now you can do ANYTHING you want to it. Either add more things (increase value) or decrease or multiply things.

I know it seems a little silly but that REALLY helped ME! :P
Was This Post Helpful? 1

#6 MAYANK7789  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-July 09

Re: Pointers in c++

Posted 21 December 2010 - 11:28 AM

View PostSlumdog, on 19 December 2010 - 08:34 PM, said:

You will understand what pointers are once you work with them. Create a small application with bunch of pointers pointing to a variable. Print the variables' addresses and print what the pointers' are pointing to. Just keep messing with 'em.

I assume you are having trouble understand pointers because you don't seem to understand addresses.

Look at this:
0x13FF60 
0x13FF64
0x13FF68
...
...
...



They are addresses; they contain a certain value. Addresses are, essentially, a "bookmark"/"address"(hence the name) in the memory where data is stored. They can be any datatype. In this case, we will work with integers. Imagine addresses are like our house or house numbers and the value the address holds is the number of people who live in the house.

Consider this:
int numOfPeopleInTheHouse = 10;
std::cout << &numOfPeopleInTheHouse; //assume 0x13FF60



That piece of code should output the address of the variable numOfPeopleInTheHouse; the address of the variable 'numOfPeopleInTheHouse', in this case, is 0x13FF60. The value that address holds 10. Lets create a pointer.

int x = 10;
std::cout << &num << std::endl;
int *p; // you can even do int *p = &num.
p = &num;
std::cout << *p << std::endl; //prints 10
*p = 9001; //the value of x is changed.
std::cout << x << std::endl; //surprise.



We created a pointer; the variable 'p' holds the address of 'x'(0x13FF60). If we dereference it using the asterisks sign, we can get the value of the address. If you do this: *p = 9001. This is, essentially, changing the value the pointer is pointing to. The value of 'x' would be 9001.

Hope that helps. Peace.






Thank you for your reply.
One more thing is ...

Can you tell me how two dimensional array is created using new operator?
If yes, I also want to know how to use it means how to add data in it and how to
retrieve data?

Was This Post Helpful? 0
  • +
  • -

#7 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Pointers in c++

Posted 21 December 2010 - 11:37 AM

See the response in your first thread.

http://www.dreaminco...th-new-keyword/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1