User: alciro    User
 Original    Translate to:  Deutsch  English  Français  中文  
 

Programación en C++ Builder

 Arrays (arrays)
 Pointers
3. Example of class in c + +
8. AnsiString class methods
 C + + projects
 Packages, distribute an application without installation
 Exchange or bubble sorting
 String string.h functions

Pointers

A pointer or an indicator is a reference to an object of a specified type in an indirect way. Basically a pointer contains the memory of a data type position.

Pointers allow to work for reference, create and manipulate dynamic objects of data structures, lists, batteries, queues, and trees.

Declaration

Pointers must be declared as pointers through the use of the unitary operator ' *' (indirection). For his statement to be a variable it is governed by the same mechanisms applied to the Declaration of any variable. As a pointer represents a variable indirection (points to the variable), declare the data type of the variable that will point to.

TipoDato * NombreVariable;

Example of Declaration of a name pointer p that will point to a variable of type int entero:

 int n; / / a variable statement whole n int * P; / / Declaration of a variable a pointer p to an integer type

The address operator &

-

Regla:

  • p =... when uses the name of the pointer is accessed the content data, the value of the variable memory address.
  • * p =... when used with the operator '*' access to the indirection, you work with the contents of the targeted memory.

Steps to working with pointers and not make mistakes.

  1. Declare certain type pointer (int * p;).
  2. Start the pointer, make sure it points to the variable (p = &...;).
  3. Work with the pointer (* p =...;).

example of pointers, copy vector

 / /-#include < vcl.h > #include < iostream.h > #include < conio.h > #pragma hdrstop / / functions prototype void copiaVector(int *v1, int *v2, int num); / /-#pragma argsused int main(int argc, char* argv[]) {/ / Variables int v [] = {3, 34, 1, 53, 15, 6};}   / / Vector 1 int x [6];   / / Vector 2 / / display the array with random numbers for(int n=0; n<6; n++) {cout < < x [n] < < "";}

copiaVector(v, x, 4);

/ / Display the vector by copying the first 4 terms cout < < endl;
for(int n=0; n<6; n++) {cout < < x [n] < < "";}

getch();
return 0;
} / /-/ * function copy vector of integer type v1 - > pointer to original vector v2 - > pointer to vector destination num - > number of elements to copy * / void copiaVector(int *v1, int *v2, int num) {for(int n=0; n<num; n++) {*(v2+n) = *(v1+n);}  {{/ / Copy the vector1 vector2 in items}}

Example of pointers, ascending from a vector

 / /-# include < vcl.h > # include < iostream.h > # include < conio.h > # pragma hdrstop # defines DIM 6 / / dimension of the array / / functions prototype void copiaVector(int_*v1,_int_*v2,_int_num); void ordenA(int_*v,_int_n); / /-# pragma argsused int main(int_argc,_char*_argv[]) {/ / variable int v [] = {3, 34, 1, 53, 15, 6};}   / / Vector 1 int x [DIM];   / / Vector 2 / / display the array with random numbers for(int_n=0;_n<6;_n++) {cout < < x [n] < < "";}

copiaVector(v,_x,_DIM);

/ / Display the vector copying terms cout < < endl;
for(int_n=0;_n<Dim;_n++) {cout < < x [n] < < "";}

ordenA(x,_DIM);

/ / Display the array ordered cout < < endl;
for(int_n=0;_n<Dim;_n++) {cout < < x [n] < < "";}

getch();
return 0;
} / /-/ * function copy vector of integer type v1 - > pointer to original vector v2 - > pointer to vector destination num - > number of elements to copy * / void copiaVector(int_*v1,_int_*v2,_int_num) {for(int_n=0;_n<num;_n++) {*(v2+n) = *(v1+n);}}  {{/ / Copy the vector1 vector2 in elements}} / * function sort Ascending array by the method of bubble * v - > pointer to the vector to order n - > number of elements in the vector * / void ordenA(int_*v,_int_n) {int j, aux; int i = 0; bool ord = false;}

/ / Ordinations while(!ord) {/ / comparisons ord = true;}
	for(j=0;j<n-1-i;j++) {if (* (v+j) > * (v+j+1)) {/ / Exchange elements aux = * (v+j);}}
			* (v+j) = * (v+j+1);
			* (v+j+1) = aux;
			Ord = false;	{{/ / Indicator of sorted vector}} i ++;
}
}
Loading
copyright © 2007-2024  www.alciro.org  All rights reserved.         
Share |