Funciones de C ++ que devuelven matrices
Soy una especie de nuevo en C ++. Estoy acostumbrado a la programación en Java. Este problema en particular me está causando grandes problemas, porque C ++ no está actuando como Java cuando se trata de Arrays. En C ++, las matrices son solo punteros.
Pero ¿por qué este código:
#include <iostream>
#define SIZE 3
using namespace std;
void printArray(int*, int);
int * getArray();
int ctr = 0;
int main() {
int * array = getArray();
cout << endl << "Verifying 2" << endl;
for (ctr = 0; ctr < SIZE; ctr++)
cout << array[ctr] << endl;
printArray(array, SIZE);
return 0;
}
int * getArray() {
int a[] = {1, 2, 3};
cout << endl << "Verifying 1" << endl;
for (ctr = 0; ctr < SIZE; ctr++)
cout << a[ctr] << endl;
return a;
}
void printArray(int array[], int sizer) {
cout << endl << "Verifying 3" << endl;
int ctr = 0;
for (ctr = 0; ctr < sizer; ctr++) {
cout << array[ctr] << endl;
}
}
imprima valores arbitrarios para verificar 2 y verifique 3. Quizás esto tenga algo que ver con la forma en que los arreglos se manejan realmente como punteros.