Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Introducing pointers

Pointers represent an important aspect of C++. Although, newbies often get confused with pointers, even those who know other programming languages.

On Monday, March 29th 2004 at 04:16 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.3 with 31 votes)
Contextual Ads
More C++ Resources
Advertisement

As a C++ newbie, it's important for you to understand pointers.

Pointer = computing computer memory address: an address, stored as data in a computer’s memory, that is the location at which the desired data is stored
Microsoft® Encarta® Reference Library 2003. © 1993-2002 Microsoft Corporation. All rights reserved.


Every variable and function has its space in the memory where it is stored.
Considering you already know the C++ data types (if not check this tutorial), every type of variable occupies a different space.
I hope you agree that a 'short' variable uses less space than an 'int' variable (although, on some systems it may be equal).

Getting the address where a variable is stored
We get the address of a variable by using a '&' prefix on the variable.

It's a matter of where and when: When we use this we won't get the value stored by the variable, we get the address of that variable, where it actually stores the data.

This example should give you a clear image of how to get the address of a variable.


// Introducing pointers

#include <iostream>
using namespace std;

int x = 12;
double y = 8.4;
int z = 4;

int main()
{
    cout << "Value of 'x' is: " << x << "\n";
    cout << "Address of 'x' is: " << &x << "\n\n";

    cout << "Value of 'y' is: " << y << "\n";
    cout << "Address of 'y' is: " << &y << "\n\n";

    cout << "Value of 'z' is: " << z << "\n";
    cout << "Address of 'z' is: " << &z << "\n\n";
    return 0;
}



Now you ask: what am I supposed to do with the address?
This you will find out at the end. We are not yet finished... actually we didn't even created a pointer yet.

Creating a pointer

A pointer is a type of variable, special for holding the address of some other variable.
In the earlier example we retrieved the address of the 'x' variable by using '&x'. Now we can store it in this special type of variable, the pointer.

As we said earlier, depending on the type, a variable can occupy more or less space in the memory. This is why when we create a pointer to a variable, we must specify the type of variable it actually points to.
Let's see an example:


int x = 12;
int * pntr = &x;



In the first line we declare a variable 'x' that holds the value '12'.
In the second line int * pntr means "pntr points to a variable of int type/size", because 'x' is an 'int' type of variable.
We actually assign the address to the pointer using '= &x'. We know that '&x' is the address of the variable 'x' and the rest of the assignment is done exactly like in an usually variable... using the '=' sign.

Now 'pntr' holds the address of the 'x' pointer, '00476748' for example.

The asterisk (*) between 'int' and 'pntr' isn't confused with the multiplication operator because of the context in which it is used.

Now it's time to say we can access the content of the 'x' variable using 'pntr'.
"How do we do that because 'pntr' stores the address, and not the content of the 'x' variable", you cry?
Answer: By using '*pntr'.
Try it:


// Accessing a variable through a pointer

#include <iostream>
using namespace std;

int x = 12;
int * pntr = &x;

int main()
{
    cout << "x = " << x << "\n";
    cout << "&x = " << &x << "\n\n";
    cout << "pntr = " << pntr << "\n";
    cout << "*pntr = " << *pntr << "\n\n";
    return 0;
}



'*pntr' is called 'accessing a variable through a pointer'... that's because we retrieve the value of 'x' by using its pointer.

What's so special about the pointer?

Here I'll answer your two big question about pointers:
"Still, what am I supposed to do with the address?"
and
"Why can't I use an usual ('int' perhaps) variable to hold the address?"

Probably you already know that a variable inside a function is different from the variable outside of that function.
If you don't already know that, check the following example:


// Variables inside and outside functions

#include <iostream>
using namespace std;

void funct(int x)
{
    cout << "(Inside) x is: " << x << "\n";
    x = 6;
    cout << "(Inside) x is changed to: " << x << "\n";
}

int main()
{
    int x = 25;
    funct(x);
    cout << "(Outside) x remains: " << x << "\n";
    return 0;
}



Is there any possible way to change the value of the variable? Here is where pointers become useful.
We know that in the earlier examples, 'pntr' had the address and '*pntr' the value of the pointed variable. By changing '*pntr' we also change 'x'.


// Using pointers with functions

#include <iostream>
using namespace std;

void funct(int& pntr)
{
    pntr = 12;
}

int main()
{
    int x = 5;
    cout << "Before the function, x is: " << x << "\n";
    funct(x);
    cout << "After the function, x is: " << x << "\n";
    return 0;
}


We answered both questions:
Q: Still, what am I supposed to do with the address?
A: Storing it in a pointer and using it for changing values inside functions, but no just that. Later in C++ you will see how pointers continue to be helpful.

Q: Why can't I use an usual ('int' perhaps) variable to hold the address?
A: Because when you change the value of the second variable, the original variable will remain the same...
If you have 'x = 5' and assign 'y = x', you will then have 'x = 5' and 'y = 5'. But if you do 'y = 12', x will still be 5.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Ehis on Thursday, June 17th 2004 at 12:17 AM

What a good tutorial!

by James on Monday, October 11th 2004 at 11:55 PM

i strongly agree with Ehis

by Tage on Tuesday, October 26th 2004 at 10:43 PM

I also agree! This made pointers a lot clearer than 4-5 other tutorials I've read COMBINED.

by NIck on Wednesday, April 27th 2005 at 05:31 PM

Needs much more detail to allow reader to see exactly how pointers are used in real life instances. i.e. linked-list, binary tree, ect... Very clear introduction to the most elementary implementaion of the pointer.

by Chris on Monday, June 13th 2005 at 12:14 PM

Great tutorial!!!!!!!!!!!!!!!!!!!!!!!!!

by Alexz11 on Saturday, September 9th 2006 at 02:43 PM

Really nice tutorial. I\'ve read so many tutorials on Pointers and I haven\'t learnt anything...until now ;)

by sorin on Wednesday, January 10th 2007 at 01:31 PM

this is a great tutorial. it helped me a lot!!!

by vinu on Tuesday, August 7th 2007 at 05:32 AM

nice

by petey on Friday, January 4th 2008 at 10:48 PM

you have a good way of explaining stuff
well done

by by jai on Wednesday, August 20th 2008 at 08:13 AM

easy example i need


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C++ Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources