8 Replies - 754 Views - Last Post: 16 August 2010 - 05:32 AM Rate Topic: -----

#1 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Pointers

Posted 14 August 2010 - 04:56 PM

im reading tutorials on c++ and i just got to pointers. im having alot of confusion. i dont understand why they exist. im pretty sure that im missing some hidden meaning at this point, but im just wondering what is the difference between this:

mypointer = &firstvalue;
  *mypointer = 10;


and this:

int firstnumber = 5;
	int secondnumber = 3;
	firstnumber = secondnumber;


if anyone with some expertise can elaborate a little on the topic, it would be much appreciated. and once again, sorry if im missing something and am terribly jacking up the subject. its just the first time i have ever dealt with something like this.

Is This A Good Question/Topic? 0
  • +

Replies To: Pointers

#2 CasGrimes  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 97
  • Joined: 09-March 10

Re: Pointers

Posted 14 August 2010 - 05:04 PM

They're good for passing by address to save up on ram usage during an application (passing a value copies the variable so in theory doubles the ram usage, so passing an int would copy it into the function using 8 bytes instead of passing it's addrress which would only use 4 bytes.
also, a pointer can be used anywhere in a program. so if it is declared in a class, it can be access somewhere else out of it's declaration scope.

Basically, if used right it is very efficient.

This post has been edited by CasGrimes: 14 August 2010 - 05:09 PM

Was This Post Helpful? 1
  • +
  • -

#3 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Pointers

Posted 14 August 2010 - 05:50 PM

With C++ you have passing by reference, by pointer, and by value. Which opposed to in C you have only by pointer and by value.

if you have say, a gigantic array you wouldn't want to pass by value, in fact in C++ you cant really pass a C style array by value, at least not overly easily.

Some class objects can't be passed by value either, take the C++ fstream class for example:

ifstream inFile;

.
.
.

void OpenFile( ifstream inFile )//<<-- error, must pass by reference.
{

.
.
.
}




The correct way to do this, is to use a reference.
void OpenFile( ifstream &inFile ) //<<-- proper way.
{
.
.
.
}



The point is, passing by value will copy the argument to the parameter of the function, and by reference and by pointer the address of the value that already exists in memory is given to the function.

So for example:

void Foo( int &n )
{
.
.
.
}

.
.
.
Foo( 3 ); //<<-- error!




Quote

mypointer = &firstvalue;
*mypointer = 10;



The '&' is the address of operator when it's used in that way, so that is storing the address of a variable in memory into that pointer.

You can create a reference pretty easily, and you can't increment a reference like you can with a pointer, the name of a C style array, this:
char buffer[512];// <<-- name is a pointer to it's first element.


The name of the array "buffer" is really just a pointer to it's first element, you can use the subscript operator "[]" to access each element, and if you don't lose the original pointer to it, you can increment the pointer.

this code accesses the first element of the array to prove my point:

*buffer = 'c';



which is exactly the same as:
buffer[0] = 'c';



with this being equal as well:
//Don't lose the address of the base.
char *arrayBegin = buffer;
buffer++;
*buffer='d';
buffer = arrayBegin;



buffer[1] = 'd';



There are reasons programmers sometimes need to remember the size of an array.

I'll be glad to help if you have any more questions.
Was This Post Helpful? 1
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,149
  • Joined: 07-September 06

Re: Pointers

Posted 14 August 2010 - 09:08 PM

Another thing to keep in mind is that this:

int first = 5;
int second = 3;
first = second;



Means that first is initialized to 5, second is initialized to 3, and then first is set to second. (and as such they both equal 3)

On the other hand this:
int first = 3;
int* second = &first;
*second = 5;



Says that first is initialized to 3, then second is set to a reference of first (a memory location) and that *second is set to 5. As a result both first and *second have the value of 5. (This is because first is the memory location that second points to, and *second, first, was set to the value 5).

Hopefully that makes some sense.
Was This Post Helpful? 1
  • +
  • -

#5 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: Pointers

Posted 14 August 2010 - 09:34 PM

thanks alot for all of the insight. these are some of the best explanations i have seen, and i have been looking around. you all deserve +10 rep at-least.
Was This Post Helpful? 0
  • +
  • -

#6 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Pointers

Posted 15 August 2010 - 05:06 PM

I usually do deserve much more. ;)
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Pointers

Posted 15 August 2010 - 06:21 PM

Pointers help you increase your code efficiency: Passing variables through pointers, overcoming scope issues, dynamic memory allocation (A Biggie), and many others...


For example lets take you calculator program that you posted here not to long ago:

http://www.dreaminco...lc/page__st__15

Since a function is just an allocated memory address, we can use pointers to simplify this

#include<iostream>

using namespace std;

double add(double a, double B)/>{return a+b;}
double sub(double a, double B)/>{return a-b;}
double mult(double a, double B)/>{return a*b;}
double div(double a, double B)/>{return a/b;}

//typedef creates an alias for another variable, this case it is a function pointer named Function
typedef double(*Function)(double, double);

int main()
{
	//Function is type of a function pointer which returns a double and accpets two doubles, we load our functions in the array
	Function methods[4] = {add, sub, mult, div};
	int selection, a, b;

	while(true) //loop till hits a break
	{
		do{
		cout<<"CALCULATOR\n[0] EXIT\n[1] ADD\n[2] SUBTRACT\n[3] MULTIPLY\n [4] DIVIDE\nENTER:";
		cin>>selection;
		}while(selection<0||selection>4); //make sure we enter the right number

		if(selection == 0)
			break;

		cout<<"Please enter your first number: \n";
		cin>>a;
		cout<<"Please enter your second number: \n";
		cin>>b;
		cout<<"The Answer is "<<methods[selection-1](a,B)/><<endl;;
	}

	return 0;
}



This is not something you will use alot (Array of Functions), but i want to show you an example of pointers on something that you have been working with


THE BIGGIE (Dynamic memory allocation)

try and compile this
int size;
cin>>size;

int myArray[size];



Does it compile?....Nope, size is not a constant therefor myArray cannot be allocated on the Stack without a known memory size;

How do we overcome this? .....Dynamic Memory Allocation

int size;
cin>>size;

//create a pointer that points to a memory location that contains a dynamically allocated array of "size" integers
int *myArray = new int[size];



WARING : when you "new" memory there must be a corresponding "delete"

Do you see how this can benefit? You can your program be more dynamic. Lets use the example of loading a file into a char array. If we dont know how big the file is than we dont know how much memory to allocate for the char array. But if we use dynamic memory we can get the size of the file during runtime than allocate the array on the fly.


SCOPE

How do we return an array?

you might think something like this right?


int *returnArray(void)
{
     int myArray[5];
     return myArray; //return the memory location of myArray
}

int main()
{  
    int *arr = returnArray();



Well even though that may look good its actually bad code due to scope bounds. lets walk through this



But honestly the list never ends....

This post has been edited by ImaSexy: 15 August 2010 - 06:35 PM

Was This Post Helpful? 2
  • +
  • -

#8 Guest_buffalobill*


Reputation:

Re: Pointers

Posted 15 August 2010 - 08:35 PM

Quote

1
mypointer = &firstvalue;
2
*mypointer = 10;

This is not valid C++ code as neither mypointer or firstvalue are accompanied by their types.
Assuming that int* mypointer had been declared as shown:
int mypointer= int &firstvalue means that it [mypointer]is pointing to the address of firstvalue.


Quote

and this:

1
int firstnumber = 5;
2
int secondnumber = 3;
3
firstnumber = secondnumber;

This means:
firstnumber is declared as an int and has been initialized with value 5
secondnumber is declared as an int and initialized with value 3
firstnumber is now assigned the value of secondnumber and now holds the value of 3.
Was This Post Helpful? 0

#9 fariba_yoo  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 89
  • Joined: 17-May 08

Re: Pointers

Posted 16 August 2010 - 05:32 AM

View Postgibson.nathan, on 14 August 2010 - 03:56 PM, said:

im reading tutorials on c++ and i just got to pointers. im having alot of confusion. i dont understand why they exist. im pretty sure that im missing some hidden meaning at this point, but im just wondering what is the difference between this:

mypointer = &firstvalue;
  *mypointer = 10;


and this:

int firstnumber = 5;
	int secondnumber = 3;
	firstnumber = secondnumber;


if anyone with some expertise can elaborate a little on the topic, it would be much appreciated. and once again, sorry if im missing something and am terribly jacking up the subject. its just the first time i have ever dealt with something like this.



mypointer = &firstvalue;
  *mypointer = 10;

in this example mypointer is points to the address of firstValue and then the value of the place where mypointer is point to is set to 10;
in other words firstValue = mypointer=10
hope this can help you about pointers.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1