将结构的 2D 数组传递给函数
passing 2D array of structure to a function
这是我的结构结构查找
{
char action;
int state;
};
行和列的值是已知的,但它们是从文件中读取的。
main()
{
// other initialization...then
lookup* table[rows][columns];
for (int i = 0; i < rows;i++)
{
for (int j = 0; j < columns;j++)
{
table[i][j]=new (lookup);
}
}
}
然后我为表的每个元素分配了值现在我想将此表传递给另一个函数以进行进一步操作说
void output(lookup* table)
{
// print values stored in table
}
如何将表及其所有内容从 main() 传递给 output() 函数?感谢您的帮助..
将参数声明为双指针(假设您收到一个一维指针数组)。由于这样的数组连续位于内存中,因此您可以计算当前元素的位置。
void output(lookup** table, int rows, int cols)
{
lookup* current_lookup = NULL;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
current_lookup = table[i*cols + j];
printf("Action: %c, state: %dn", current_lookup->action, current_lookup->state);
}
}
}
您可以通过传递数组的第一个元素来调用它:
int main()
{
lookup* table[rows][columns];
//....
output(table[0]);
return 0;
}
相关文章:
- 第 i 个元素返回 i 的函数数组
- 使用嵌套函数数组是个好主意吗?
- 正确实现类函数数组
- 试图引用已删除函数数组的相等运算符
- 如何在静态函数中使用成员函数数组
- 无法创建函数数组
- 构造函数数组
- 使用开关或定义函数数组 - 更快
- 编写一个函数,该函数将覆盖主函数数组中的值
- 可以像'funs[1]();'一样调用的函数数组
- std::AVX内部函数数组
- 如果数组大小未知,如何将函数数组作为参数传递
- 从函数数组中获取值
- 使用元编程在编译时初始化函数数组
- 将C函数数组转换为C++函数指针数组
- 复制构造函数 [数组] C++
- 添加函数数组并随机选择其中一个
- 指针到私有成员函数数组
- 可以单独声明函数数组的元素
- (c++)将main中的随机值保存到函数数组中