C/C++ Vector and reference parameter -
i want receive vector in main function. code this.
int myfunction(void); int main(){ int p = myfunction(void); std::cout << p[2] << std::endl; }; int myfunction(void){ int new array[4]={0,1111,2222,3333}; int *p; p = array; return p; };
in c++ do:
std::vector<int> myfunction(); int main(){ std::vector<int> p = myfunction(); std::cout << p[2] << std::endl; } std::vector<int> myfunction(){ return std::vector<int>{0,1111,2222,3333}; }
and in c do:
int* myfunction(void); int main(void){ int* p = myfunction(); printf("%d\n", p[2]); free(p); } int* myfunction(void){ int tmp[] = {0,1111,2222,3333}; int* array = (int*)malloc(sizeof(tmp)); memcpy(array, &tmp, sizeof(tmp)); return array; }
now if have trouble code, i'd recommend go pick c or c++ book (whichever you're interested in) , read on basics of language, because seem confused.
Comments
Post a Comment