Passing a non dynamic 2 dimensional array into a function in C?
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);
开发者_JS百科
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.
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?
精彩评论