C++ &array和array的用法有什么区别?
C++ What is the difference between the usage of &array and array?
最近,当我需要将其传递给C 中的另一个函数时,我遇到了使用数组地址的问题。例如:
void do_something(float * arr, int size) {
//Will do something about the arr
}
int main () {
float array[] = {1, 2, 3, 4};
do_something(array, 4); // this will work well
do_something(&array, 4); // this will cause error
return 0;
}
但是,当我尝试同时打印出数组和Amp;数组时,它们是相同的。你们知道这样做的原因是什么?
这是一种使用std::vector
:
#include <vector>
void do_something(const std::vector<float>& arr) {
// Use arr for whatever.
}
int main() {
std::vector<float> arr = { 1, 2, 3, 4 };
do_something(arr);
return 0;
}
此初始化器需要C 11模式,如果该标志已打开,大多数编译器都支持。
相关文章:
- C++如果必须在编译时确定大小,std::array 有什么意义?
- 向后迭代 std::array 或 std::vector 的正确方法是什么?
- 通过作为指向 C++ 函数的指针传递来访问 std::array 元素的正确方法是什么?
- scanf( "%d" , array[i] + 1)是什么意思?为什么+1在那里?
- 错误"invalid types ... for array subscript"是什么意思?
- 警告的原因是什么:"when type is in parentheses, array cannot have dynamic size"?
- "std::tuple_size<std::array>"的目的是什么
- C++ &array和array的用法有什么区别?
- 如何知道您输入到std::array中的最后一个元素是什么?
- 当您声明"pointer type"形式(函数)参数"like an const array"时,什么是常量?
- 什么是指向 C 语言中'array of characters'的指针?
- "except that a default constructed array is not empty"是什么意思?
- 0-length数组(或std::array)有什么用
- "address of array"的实际用例是什么?
- std::array 作为字段的语法是什么,里面的对象必须使用接受参数的 ctor 构造
- std::valarray和std::array之间有什么区别
- std::array的内联初始化有什么问题?
- 初始化的Array构造函数做什么?
- 对于编译时的1.constructor和2.array定义,几乎没有什么疑问
- std::array和std::vector的区别是什么?什么时候你用一个而不是另一个