www.devguides.com | The developers choice   

 
Technologies available 
    Apache (1)
    ASP (0)
    ASP.NET (0)
    C&C++ (1)
    Google (1)
    HTML (1)
    Java (0)
    JavaScript (1)
    PERL (0)
    PHP (3)
    Unix (0)
    Visual Basic (0)
    XML (0)

Databases 
    MySQL (0)
    Oracle (0)
    PostgreSQL (0)
    SQL Server (1)

Security against 
    Backdoors (0)
    Malicious Mobile Code (0)
    Trojan Horses (0)
    Viruses (0)
    Vulnerabilities (0)

 
 You are currently reading

Pointers explained Part 1.
posted by Andres @ 12:32 PM | Tuesday, May 04, 2004
C++ is one of the best Computer programming languages; Ergo, developers must understand how pointers work in C++ because pointers gives you the power to develop more efficient computer programs among other things.


 The article requested:
Before we deal with some coding.
In this article I'll show you one of the most important and powerful characteristics of C++, pointers. Every person that studies pointers always tend to get into trouble because pointers aren't easy if your experience level is 0 or closer to it. Anyhow, pointers gives the power to manipulate data. If you already know pointers I would recommend you to also read the article because not everybody actually tells you what things could you do with pointers. Pointers are also best used in order to acomplish things that can't be done with standard variables. (Don't panic, keep reading)

What is a pointer?
A pointer is practically a variable that holds an address (memory address). I recommend to you to learn a bit more about computer memory in order to understand this but I'll cover it here though. In a computer program, each variable you create is located at a location in memory (unique, each variable has differente places) and they are known as address. When you program anything and you create your variables, you actually don't know the addresses of each variable and it's not very important because the compiler deals with it, but you could also find out the address of a variable by using the & operator. Here is a little example:

1 #include <iostream.h>
2
3 void main() {
4     unsigned short var1 = 5;
5     unsigned long var2 = 12345;
6
7     cout << "var1 = " << var1 << endl;
8     cout << "Address of var1 = " << &var1 << endl;
9
10   cout << "var2 = " << var2 << endl;
11   cout << "Address of var2 = " << &var2 << endl;
12}

If we run this program, it will give us 2 variables addresses.

1 #include <iostream.h>

Line 1 I'm calling the necessary library in order to tell the program to send data to the screen.

3 void main() {

In this line I'm creating the main function, this function is a must for every computer program created by C or C++. You can also notice that after the parenthesis I'm using brackets, everything you open, you must close it.

4     unsigned short var1 = 5;
5     unsigned long var2 = 12345;
6
7     cout << "var1 = " << var1 << endl;
8     cout << "Address of var1 = " << &var1 << endl;
9
10   cout << "var2 = " << var2 << endl;
11   cout << "Address of var2 = " << &var2 << endl;

In lines 4 and 5 I'm declaring and initializing two variables after that I'm using the cout keyword in order to tell C++ to send the output to the screen for each line, read carefully. I also used the endl keyword which is the same as \n (means a new line). Finally in the last line I closed the main function and we now have our first program that retrieves addresses of variables.

[1][2][Storing the address in pointers]


Copyright ©2004 Andrab, Inc. All rights reserved.