El siguiente es un ejemplo sobre el manejo de punteros, compilado en Visual C++
La salida del programa es así:
#include <iostream>
using namespace std;
#define NUM_ELEM 10
int main(void)
{
// Definimos la estructura de datos POINT
typedef struct
{
int ix;
int iy;
}POINT;
// Creamos un array de memoria dinamica referenciado por pArPoint
POINT * const pArPoint = new POINT[NUM_ELEM];
POINT * pBuffer = pArPoint;
int i;
// Cargamos el array de datos
for(i = 0; i < NUM_ELEM; i++)
{
pBuffer->ix = i * 10;
pBuffer->iy = i * 10 + 10;
cout << pBuffer << "\t" << pBuffer->ix << "\t" << pBuffer->iy << endl;
pBuffer++;
}
cout << "\n";
// Apuntar el buffer al inicio
pBuffer = pArPoint;
//Creamos un nuevo array de datos
POINT arPoint[NUM_ELEM];
// Copiamos los datos al nuevo array de datos
for(i = 0; i < NUM_ELEM; i++)
{
arPoint[i].ix = pBuffer->ix;
arPoint[i].iy = pBuffer->iy;
cout << arPoint << "\t" << arPoint[i].ix << "\t" << arPoint[i].iy << endl;
pBuffer++;
}
// Liberar la memoria
delete []pArPoint;
// presionar una tecla para salir
system("pause");
return EXIT_SUCCESS;
}
Pueden descargar el archivo fuente aquí abajo: