Los punteros son representaciones simbólicas de direcciones. Permiten que los programas simulen llamadas por referencia, así como que creen y manipulen estructuras de datos dinámicas. Su declaración general en C/C++ tiene el formato:
Sintaxis:
C
// C++ program to illustrate Pointers in C++ #include <stdio.h> void geeks() { int var = 20; // declare pointer variable int *ptr; // note that data type of ptr and var must be same ptr = &var; // assign the address of a variable to a pointer printf("Value at ptr = %p \n",ptr); printf("Value at var = %d \n",var); printf("Value at *ptr = %d \n", *ptr); } // Driver program int main() { geeks(); }
C++
// C++ program to illustrate Pointers in C++ #include <bits/stdc++.h> using namespace std; void geeks() { int var = 20; //declare pointer variable int *ptr; //note that data type of ptr and var must be same ptr = &var; // assign the address of a variable to a pointer cout << "Value at ptr = " << ptr << "\n"; cout << "Value at var = " << var << "\n"; cout << "Value at *ptr = " << *ptr << "\n"; } //Driver program int main() { geeks(); }
C
// C program to illustrate call-by-methods #include <stdio.h> void geeks() { //Declare an array int val[3] = { 5, 10, 20 }; //declare pointer variable int *ptr; //Assign the address of val[0] to ptr // We can use ptr=&val[0];(both are same) ptr = val ; printf("Elements of the array are: "); printf("%d %d %d", ptr[0], ptr[1], ptr[2]); } //Driver program int main() { geeks(); }
C++
// C++ program to illustrate Array Name as Pointers in C++ #include <bits/stdc++.h> using namespace std; void geeks() { //Declare an array int val[3] = { 5, 10, 20 }; //declare pointer variable int *ptr; //Assign the address of val[0] to ptr // We can use ptr=&val[0];(both are same) ptr = val ; cout << "Elements of the array are: "; cout << ptr[0] << " " << ptr[1] << " " << ptr[2]; } //Driver program int main() { geeks(); }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA