Advertisement
xlujiax

LP1

Oct 18th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char** argv) {
  8.     void *ptrA, *ptrB;
  9.     int a = 10; // Un tipo de dato
  10.     ptrA = &a;
  11.     int*b = new int;
  12.     *b = 20;    // Una variable de tipo puntero a entero
  13.     ptrB = b;
  14.    
  15.     cout << "Prueba: void* a int* (unico elem.)\n";
  16.     cout << *(int*)ptrA << endl;    
  17.     cout << *(int*)ptrB << endl;  
  18.    
  19.     int *c  = new int[5];
  20.     for (int i = 0; i < 5; i++) {
  21.         c[i]= i+1;
  22.     }
  23.    
  24.     void *ptrC;
  25.     ptrC = c;
  26.    
  27.     cout << *(int*)ptrC << endl;  //Apunta solo a la cabecera
  28.    
  29.     void**ptrRec = (void**)ptrC;
  30.     for (int i = 0; i < 5; i++) {
  31.         cout << i << ") ";
  32.         cout << *((int*)ptrRec+i) << " " << endl; //Aritmetica de ptros
  33.         cout << ((int*)ptrRec)[i] << " " << endl; //Operador de acceso
  34.        
  35.         int *auxInt = ((int*)ptrRec+i);   //Aritmetica de ptros
  36.         int *auxInt = &((int*)ptrRec)[i]; //Operador de acceso
  37.          
  38.         cout << *auxInt << endl;
  39.     }
  40.  
  41.    
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement