C/C++ 矢量和参考参数
C/C++ Vector and reference parameter
我想在我的主函数中接收一个 VECTOR。代码是这样的。
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;
};
在C++中,你会做:
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};
}
在 C 中,您可以执行以下操作:
int* myfunction(void);
int main(void){
int* p = myfunction();
printf("%dn", 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;
}
现在,如果你在这段代码上遇到问题,我建议你去挑选一本好的C或C++书(无论你对哪个感兴趣)并阅读该语言的基础知识,因为你看起来真的很困惑。
相关文章:
- Lambda闭包左值可以作为右值参考参数传递
- 遍历地图并将该对用作参考参数 C++11
- 什么是常量参考参数以及如何使用它?
- 如何将rvalue作为参考参数传递给函数
- 移动 l 值参考参数是否是一种不好的做法?
- C 11:为什么RVALUE参考参数隐式转换为LVALUE
- 为什么可以将其他类型变量用作C 中常量参考参数参数的参数
- 如何处理可能指向内部数据的参考参数
- C 功能具有参考参数错误的迭代器错误.寻求解释
- 用emscripten中的参考参数绑定函数
- 一种体面的方式来转换const列表参考参数,然后传递到另一个函数
- 参考参数和递归
- 分配运算符过载:返回void与返回参考参数
- C ,构造器中允许的参考参数默认值
- Qt信号和插槽:是复制的参考参数
- C 接收const lvalue和rvalue参考参数,而无需过载
- std ::移动返回和输入参考参数
- 参考参数返回副本
- 如何使用数组和参考参数调用void函数
- 按值传递参考参数