Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,615 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 968 people online right now. Registration is fast and FREE... Join Now!




Why use pointers? I don't get it at all.

 
Reply to this topicStart new topic

Why use pointers? I don't get it at all.

Dancia
post 15 Jul, 2008 - 03:50 PM
Post #1


D.I.C Head

**
Joined: 15 Jun, 2008
Posts: 57



Thanked 1 times
My Contributions


First. I've got 3 years of programming PHP and year Pascal and I never saw something like pointer.

Ok I have read:
http://www.cplusplus.com/doc/tutorial/pointers.html

CODE

// my first pointer
#include <iostream>
using namespace std;

int main ()
{
  int firstvalue, secondvalue;
  int * mypointer;

  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;
  return 0;
}


can't I just write?
CODE

  mypointer = firstvalue;
  mypointer = 10;
  mypointer = secondvalue;
  mypointer = 20;


WHY should I use em at all?

+ read http://www.codersource.net/c++_pointers.html
Ok I'll try to guess:
Maybe then I set pointer to value If I change pointer = 1 then the other value will be 1 too?
User is offlineProfile CardPM

Go to the top of the page

red_4900
post 15 Jul, 2008 - 06:14 PM
Post #2


Code Dreamers

****
Joined: 22 Feb, 2008
Posts: 792



Thanked 10 times
My Contributions


Okey~ I'm going to assume you're a beginner in C/C++, although you have been programming PHP for a much longer time than me. Let me give you an easier example. Compare them. This is when you don't use pointers :
cpp
#include<iostream>
using namespace std;
void swap(int ,int );

int main(void){
int x,y;
x=10;
y=15;

//print the value of x and y
cout<<"x is now "<<x<<endl;
cout<<"y is now "<<y<<endl;

swap(x,y); //swap!!
cout<<endl;

//let's double check, shall we?
cout<<"x is now "<<x<<endl; //......
cout<<"y is now "<<y<<endl; //......it didn't swap??

return 0;
}

void swap(int x,int y){
int temp;
//let's swap the value between x and y
temp=x;
x=y;
y=temp;

cout<<endl<<"now in function swap and after we swap the value between x and y"<<endl;
cout<<"x is now "<<x<<endl;
cout<<"y is now "<<y<<endl;
//okey.... so we swapped the value between x and y, didn't we?
}


as opposed when you use pointers :
cpp
#include<iostream>
using namespace std;
void swap(int *,int *);

int main(void){
int x,y;
x=10;
y=15;

//print the value of x and y
cout<<"x is now "<<x<<endl;
cout<<"y is now "<<y<<endl;

//swap!!
swap(&x,&y);
cout<<endl;

cout<<"x is now "<<x<<endl; //it swapped!!!
cout<<"y is now "<<y<<endl; //see? it worked!!

return 0;
}

void swap(int *px,int *py){
int temp;

//let's swap the value between x and y
temp=*px;
*px=*py;
*py=temp;
}


hope this helps you smile.gif
User is offlineProfile CardPM

Go to the top of the page

polymath
post 15 Jul, 2008 - 07:02 PM
Post #3


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


Also, you can only create an array of dynamic legnth using a pointer. if you have variable legnth, and you want to create an array of ints with number of elements legnth, you need:

int * array=new int[legnth];

then:

delete[] array;

Or, you could use vectors, except that is really just pointers in disguise smile.gif
User is offlineProfile CardPM

Go to the top of the page

Einherjar
post 15 Jul, 2008 - 07:05 PM
Post #4


D.I.C Head

**
Joined: 10 Feb, 2008
Posts: 73


My Contributions


Also very nice to use when passing large objects around. Much easier to pass the address than copy the whole thing in memory.
User is offlineProfile CardPM

Go to the top of the page

nirvanarupali
post 15 Jul, 2008 - 11:01 PM
Post #5


D.I.C Foot

Group Icon
Joined: 1 Aug, 2007
Posts: 983



Thanked 2 times

Dream Kudos: 375
My Contributions


One of the most powerful tools available to a C++ programmer is the ability to manipulate computer memory directly by using pointers.

A pointer is a variable that holds a memory address and to understand pointers, you must know a little about computer memory.

They can be somewhat confusing, and it isn't immediately obvious why they are needed.

Why use pointers?

Ans:

1.) Managing data on the free store.

2.) Accessing class member data and functions.

3.) Passing variables by reference to functions.



User is offlineProfile CardPM

Go to the top of the page

captainhampton
post 16 Jul, 2008 - 05:09 AM
Post #6


Jawsome++;

Group Icon
Joined: 17 Oct, 2007
Posts: 518



Thanked 2 times

Dream Kudos: 825
My Contributions


Pointers are quite prominent in real-life programming, the concept may seem a little strange coming from your particular background but don't let that leave you jaded on the concept of pointers. They are a very powerful tool in the world of C++
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 02:52AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month