Pointer
Address
#include <iostream>
int main(int argc, char *argv[])
{
int x = 25;
int *ptr = &x;
std::cout<<"Value: "<<*ptr<<std::endl;
std::cout<<"Address: "<<ptr<<" "<<std::endl;
return 0;
}
- &, return the memory address of a variable
#include <iostream>
int main(int argc, char *argv[])
{
int x = 25;
int *ptr = &x;
*ptr += 100;
std::cout<<"Value: "<<*ptr<<std::endl;
return 0;
}
Array and Pointer
#include <iostream>
template <class T>
void init(T *array, int size)
{
//use pointer as array
for(int i = 0; i < size; i++)
array[i] = i*10;
}
template <class T>
void display(T *array, int size)
{
for(int i = 0; i < size; i++)
std::cout<<*(array+i)<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
const int size = 10;
int array[size];
init(array, size);
display(array, size);
//use array as pointer
for(int i = 0; i < size; i++)
std::cout<<*(array+i)<<" ";
std::cout<<std::endl;
return 0;
}
- When add a value to a pointer, actually adding that value times the size of the data type being referenced by the pointer
Pointer Arithmetic
#include <iostream>
int main(int argc, char *argv[])
{
const int size = 5;
int array[] = {10, 20, 30, 40, 50};
int *ptr;
ptr = array;
for(int i = 0; i < size; i++)
std::cout<<*ptr++<<" ";
std::cout<<std::endl;
ptr = array;
std::cout<<*(ptr+3)<<std::endl;
return 0;
}
Pass to Function
#include <iostream>
template <class T>
T getNumber()
{
T n;
std::cout<<"Enter a number:";
std::cin>>n;
return n;
}
template <class T>
void doubleNum(T *n)
{
*n *= 2;
}
int main(int argc, char *argv[])
{
int num;
num = getNumber<int>();//explicitily select template since no T appears in function's parameter list
doubleNum(&num);
std::cout<<"Num: "<<num<<std::endl;
return 0;
}
#include <iostream>
template <class T>
void init(T *array, int size)
{
for(int i = 0; i < size; i++)
*array++ = i*10;
}
template <class T>
void display(T *array, int size)
{
for(int i = 0; i < size; i++)
std::cout<<*array++<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
const int size = 5;
int array[size];
init(array, size);
display(array, size);
return 0;
}
*array++, * operator defference array firstly, then ++ operator will increment the address in array
Tranverse
#include <iostream>
#include <iterator>
#include <cstdlib>
template <class T>
void init(T it, T end)
{
for(; it != end; ++it)
*it = rand()%100;
}
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
int *ptr = new int[10];
init(ptr, ptr+11);
display(ptr, ptr+11);
return 0;
}
Pointers to Constants
#include <iostream>
template <class T>
void init(T *array, int size)
{
for(int i = 0; i < size; i++)
*array++ = i*10;
}
template <class T>
//void increase(const T *array, int size)//compile error
void increase(T *array, int size)
{
for(int i = 0; i < size; i++)
*array++ += 1;
}
template <class T>
void display(const T *array, int size)
{
for(int i = 0; i < size; i++)
std::cout<<*array++<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
const int size = 5;
double array[size];
init(array, size);
increase(array, size);
display(array, size);
return 0;
}
Pointers to Constants and Constant Pointers
#include <iostream>
//pointer to const
void change1(const int *ptr)
{
//*ptr += 1;//compile error
}
//constant pointer
void change2(int * const ptr)
{
//ptr = 0;//compile error
}
//constant pointers to constants
void change3(const int * const ptr)
{
std::cout<<*ptr<<std::endl;
}
int main(int argc, char *argv[])
{
int v = 10;
change1(&v);
change2(&v);
change3(&v);
return 0;
}
- pointer to const points to a constant item, the data that pointer points to cannot change, but the pointer itself can change
- const pointer, the pointer itself is constant, once the pointer is initialized with an address, it cannot point to anything else
Dynamic Memory Allication
#include <iostream>
#include <cstdlib>
int main(int argc, char *argv[])
{
int *ptr;
ptr = new int;
if (ptr == NULL)
{
std::cerr<<"Error on Allocating Memory ..."<<std::endl;
exit(1);
}
delete ptr;
ptr = 0;
return 0;
}
#include <iostream>
template <class T>
void init(T *array, int size)
{
for(int i = 0; i < size; i++)
array[i] = i*10;
}
template <class T>
void display(T *array, int size)
{
for(int i = 0; i < size; i++)
std::cout<<*array++<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
int size;
std::cout<<"Enter array size: "<<std::endl;
std::cin>>size;
//allocate memory
int *array = new int[size];
init(array, size);
display(array, size);
//release memory
delete [] array;
array = 0;
return 0;
}
array = 0
- prevent code to access the freed memory
- prevent erros from occurring if delete is accidentlly called on the pointer again
Returning Pointers from Function
#include <iostream>
int *getArray(int size)
{
int *array = new int[size];
return array;
}
void init(int *array, int size)
{
for(int i = 0; i < size; i++)
array[i] = i*10;
}
void display(int *array, int size)
{
for(int i = 0; i < size; i++)
std::cout<<array[i]<<" ";
std::cout<<std::endl;
}
void freeArray(int *array)
{
delete [] array;
array = 0;
}
int main(int argc, char *argv[])
{
const int size = 10;
int *array;
array = getArray(size);
init(array, size);
display(array, size);
freeArray(array);
return 0;
}
Only return a pointer from a function if it is
- a pointer to an item that was passed into the function as an argument
- a pointer to a dynamically allocated chunk of memory