从二维动态数组中获取长度
C++ get length from two dimensional dynamic array
我被c++二维动态数组卡住了。我想要得到数组长度。下面是代码:
#include <iostream>
using namespace std;
int dosomestuff(char **dict);
int main(){
int x, y;
char **dict;
cin>>x>>y; // here to input the 'x'
dict = new char *[x];
for(i = 0; i < x; i++){
dict[i] = new char[y];
for(j = 0; j < y; j++){
cin>>dict[i][j];
}
}
dosomestuff(dict);
}
int dosomestuff(char **dict){
int x, y;
x = sizeof(*dict); //8 not equal to the 'x'
//run in mac_64 I think this is the pointer's length
y = strlen(dict[0]); //this equal to the 'y' in function main
cout<<x<<" "<<y<<endl;
return 0;
}
我想要的是在函数dosomestuff中得到x等于函数main中的'x'
我怎样才能得到它?有人能帮帮我吗?非常感谢。
sizeof(*dict)
只给你sizeof(char*)
,这不是你所希望的。
无法从dosomestuff
中的dict
得知x
的值。如果您想为dict
使用char**
,您最好的选择是将x
和y
传递给dosomestuff
。
int dosomestuff(char **dict, int x, int y);
因为你正在使用c++,你可以使用:
std::vector<std::string> dict;
如果你将dict
传递给它,那么你将拥有dosomestuff
中所需的所有信息。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 使用指针从C++中的数组中获取最大值
- 获取字符串的长度并将其分配给数组
- 数组长度,为什么从命令行获取时不能使用它?
- 如何在 nlohmann 的 json 库中获取数组长度?
- 在c++中获取两个大int,并将它们存储在数组中
- 如何在C++中获取二维数组中最少的一列数?
- 从 numpy 数组获取指针以将图像发送到C++
- 指针数组.获取指针元素的值
- 循环以使用 cstring 数组 c++ 获取用户输入
- 一种从元组/数组获取参数包的方法
- 如何使用数组获取输入,并将其存储在record.h类的主类中的变量中
- 如何使用数组获取命令行参数
- 从 JSON 数组获取天气
- 布尔数组获取意外的默认值
- 填充列表<>使用数组获取错误的数字
- 如何在C++中为矢量数据结构中的二维数组获取用户输入,而不使用行和列上的两个for循环
- 在自定义库中操作2d C++数组-获取行数
- 使用数组获取测试结果
- 数组c++获取值