#include <stdio.h>
#include <iostream>
using namespace std;
void swap_numbers(int *num1, int *num2);
int main()
{
int num;
int *pNum;
pNum = #
*pNum = 8;
cout << "num = " << num << endl << endl;
int num1 = 1;
int num2 = 2;
swap_numbers(&num1, &num2);
cout << "num1 = " << num1 << endl << "num2 = " << num2;
getchar();
return 0;
}
void swap_numbers(int *num1, int *num2)
{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
/*
This tutorial demonsterates the use of pointers.
Pointers are one of the most hated and loved subjects in c++.
It's one of the things that separates c++ from the other
programming languages. If you handle pointers right, you will be able to
increase the speed of your applications, and decrease the amount of used memory.
When you see this symbol * in c++ it means that a pointer is used.
When we declare a variable it's really just a series of memory cells,
that we can acess by an identifier.
example: a short int uses 16 bit (2 byte), a long int uses 32 bit (4 byte)
Let say that when you declare int myVariable; it uses 32 bit,
and the variable gets an unique address in memory.
A good simile for the computer memory can be a street in a city.
On a street all houses are consecutively numbered with an unique
identifier so if we talk about 27th of Sesame Street we will be
able to find that place without loss, since there must be only one
house with that number and, in addition, we know that that house
will be between houses 26 and 28.
In the same way in which houses in a street are numbered,
the operating system organizes the memory with unique and
consecutive numbers, so if we talk about location 1776 in the
memory, we know that there is only one location with that address
and also that is between addresses 1775 and 1777.
Using a pointer we can access directly to the value stored in
the variable pointed by it just by preceding the pointer identifier
with the reference operator asterisk (*), that can be literally
translated to "value pointed by".
declare a pointer:
int *pNum;
define the pointer(let the pointer point to):
pNum = #
*/
|