将非动态二维数组传递给 C 中的函数?
如何将二维数组传递给函数?
当我传递它时,它在 Visual Studio 调试器中显示为一维数组。 问题是,当我索引 sel_col[i] 时,它获取下一个字符,而不是下一个单词。
这是我的函数原型:
void print_row(char sel_col[MAX_IDENT_LEN][MAX_IDENT_LEN]);
这是我的二维数组的定义,以及我在哪里调用它:
char sel_col[MAX_NUM_COL][MAX_IDENT_LEN];
print_row(sel_col);
解决方案 1: 删除第一个大小标识符。没用。
void print_row(char sel_col[][MAX_IDENT_LEN]);
解决方案 2:通过引用将其传递到函数中。没用。
print_row(&sel_col);
解决方案 3: 使用双指针。我不想走这条路,因为我会重写大部分代码。
How do you pass two dimensional arrays into a function?
When I pass it, it shows up in the visual studio debugger as a single dimensional array.
The problem was that when I index sel_col[i], it gets the next character, instead of the next word.
Here is my function prototype:
void print_row(char sel_col[MAX_IDENT_LEN][MAX_IDENT_LEN]);
And here is the definition of my 2d array, and where I call it:
char sel_col[MAX_NUM_COL][MAX_IDENT_LEN];
print_row(sel_col);
Solution 1: Remove the first size identifier. Didn't work.
void print_row(char sel_col[][MAX_IDENT_LEN]);
Solution 2: Pass it by reference into the function. Didn't work.
print_row(&sel_col);
Solution 3: Use double pointers. I don't want to go this route, since I would rewrite most of my code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 C 语言中,二维数组看起来像一维数组;
char[3][3]
实际上是一个只有 9 个元素长的一维数组,编译器会执行一些数学运算将两个较小的索引转换为一个较大的索引。您的函数原型看起来是正确的。问题出在哪里?您收到编译器错误吗?In C, two-dimensional arrays look like one dimensional arrays; a
char[3][3]
is really a single-dimension array just 9 elements long, and the compiler performs some math to turn two smaller indices into one bigger one. Your function prototype looks correct. What was the problem? Were you getting a compiler error?