C# 如何在 C 中初始化多维字符数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/845089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Initialize a Multidimensional Char Array in C?
提问by CodeConfused
I'm trying to convert some code from C# to C so that it can be burnt onto a microcontroller.
我正在尝试将一些代码从 C# 转换为 C,以便可以将其烧录到微控制器上。
Could someone please tell me how I would convert a two dimensional string array in C# into something in C?
有人可以告诉我如何将 C# 中的二维字符串数组转换为 C 中的内容吗?
My C# code looks like this:
我的 C# 代码如下所示:
string[,] DirectionPosition = {{"00", "10", "", "01", ""},
{"01", "11", "", "02", "00"},
{"02", "12", "", "03", "01"},
{"03", "13", "", "04", "02"},
{"04", "14", "", "", "03"},
{"10", "20", "00", "11", ""},
{"11", "21", "01", "12", "10"},
{"12", "22", "02", "13", "11"},
.
.
.
.
{"44", "", "34", "", "43"},};
And moreover, how would I access the elements? In C# if I wanted the second element in the third row it would simply be DirectionPosition[2,1] but what is the equivalent of that when there is no string in C much less 2-D string arrays?
此外,我将如何访问这些元素?在 C# 中,如果我想要第三行中的第二个元素,它只是 DirectionPosition[2,1],但是当 C 中没有字符串而不是二维字符串数组时,它的等价物是什么?
采纳答案by dirkgently
char DirectionPosition[][ 5 ][ 3 ] = {{"00", "10", "", "01", ""},
{"01", "11", "", "02", "00"},
{"02", "12", "", "03", "01"},
{"03", "13", "", "04", "02"},
{"04", "14", "", "", "03"},
{"10", "20", "00", "11", ""},
{"11", "21", "01", "12", "10"},
{"12", "22", "02", "13", "11"},
.
.
.
.
{"44", "", "34", "", "43"},};
C has no inbuilt string class, you have to make do with character arrays. You could alternatively use pointers to pointer to char.
C 没有内置的字符串类,您必须使用字符数组。您也可以使用指向 char 的指针。
回答by paxdiablo
The easiest way to do it is with char pointers as follows:
最简单的方法是使用 char 指针,如下所示:
char *DirectionPosition[9][5] = {
{"00", "10", "", "01", "" },
{"01", "11", "", "02", "00"},
{"02", "12", "", "03", "01"},
{"03", "13", "", "04", "02"},
{"04", "14", "", "", "03"},
{"10", "20", "00", "11", "" },
{"11", "21", "01", "12", "10"},
{"12", "22", "02", "13", "11"},
{"44", "", "34", "", "43"}
};
The element "10" in the first line is referenced as DirectionPosition[0][1]
(zero-based, first index is line, second is column).
第一行中的元素“10”被引用为DirectionPosition[0][1]
(从零开始,第一个索引是行,第二个是列)。
回答by Alex Martelli
I recommend deciding the maximum length of strings and maximum number of strings per row and telling the compiler, e.g.:
我建议决定字符串的最大长度和每行的最大字符串数并告诉编译器,例如:
typedef char string[3];
typedef string s5[5];
s5 DirectionPosition[] = {{"00", "10", "", "01", ""}, {"01", "11", "", "02", "00"}, {"02", "12", "", "03", "01"}, {"03", "13", "", "04", "02"}, {"04", "14", "", "", "03"}, {"10", "20", "00", "11", ""}, {"11", "21", "01", "12", "10"}, {"12", "22", "02", "13", "11"}, {"44", "", "34", "", "43"},};
Now, DirectionPosition[2][1] &c will let you access specific strings in the matrix.
现在, DirectionPosition[2][1] &c 将允许您访问矩阵中的特定字符串。
回答by Robert S. Barnes
There's a slightly simpler way of doing this without all the extra brackets:
没有所有额外的括号,有一种稍微简单的方法可以做到这一点:
#include <stdio.h>
int main(int argc, char **argv) {
char DirectionPosition[][ 5 ][ 3 ] = {"00", "10", "", "01", "",
"01", "11", "", "02", "00",
"02", "12", "", "03", "01",
"03", "13", "", "04", "02",
"04", "14", "", "", "03",
"10", "20", "00", "11", "",
"11", "21", "01", "12", "10",
"12", "22", "02", "13", "11",
"44", "", "34", "", "43"};
printf("dp[1][1] == %s\n", DirectionPosition[1][1]);
printf("dp[1][2] == %s\n", DirectionPosition[1][2]);
printf("dp[1][3] == %s\n", DirectionPosition[1][3]);
return;
}