Beruflich Dokumente
Kultur Dokumente
1
Direcciones de Memoria
2
Direcciones de Memoria
int x;
x = 3;
int* y;
y = &x;
4
Ejemplo: Obteniendo la direccin de
una variable
#include <iostream>
int main()
{
int x;
x = 3;
int* y;
y = &x;
cout << "Direccion : " << y << " = " << (long long)(y) << endl;
cout << "Tamanio de direccion : " << sizeof(y) << " bytes = " << sizeof(y) * 8 << " bits " <<endl;
return 0;
}
5
Operadores
Operador &
#include <iostream>
El operador de
using namespace std;
direccin (&) retorna la
int main()
direccin (o puntero) {
/* Declaracion de variable entera */
int x = 3;
/* Nuevo valor */
cout << x << endl;
return 0;
Operador * }
El operador de
indireccin (*) retorna
el valor
6
int x = 3, y = 2;
7
int x = 3, y = 2;
*ptr = 7; // x = 7
8
int x = 3, y = 2;
*ptr = 7; // x = 7
*ptr2 = *ptr2 + 1 // x = 8
9
int x = 3, y = 2;
*ptr = 7; // x = 7
*ptr2 = *ptr2 + 1 // x = 8
ptr = &y;
*ptr2 = 10 // x = 10;
10
Punteros y Arreglos: Aritmtica de Punteros
Entonces:
arr -> puntero a arr[0] arr -> puntero a arr[0]
arr + i -> puntero a arr[i] arr + i -> puntero a arr[i]
avanza de 4 bytes por elemento avanza 2 bytes por elemento
(sizeof(int) = 4) (sizeof(int) = 2)
direccin direccin
12
http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/pointer.html
Ejemplo: Validar palindromos
#include <iostream>
#include <cstring>
ptr_inicio++;
ptr_fin--;
}
return true;
}
int main() {
return 0;
}
13