我有以下数组:
int A[] = {0,1,1,1,1, 1,0,1,0,0, 0,1,1,1,1};
int B[] = {1,1,1,1,1, 1,0,1,0,1, 0,1,0,1,0};
int C[] = {0,1,1,1,0, 1,0,0,0,1, 1,0,0,0,1};
//etc... for all letters of the alphabet
Run Code Online (Sandbox Code Playgroud)
还有一个在5x3 LED矩阵上打印字母的功能:
void printLetter(int letter[])
Run Code Online (Sandbox Code Playgroud)
我有一串字母:
char word[] = "STACKOVERFLOW";
Run Code Online (Sandbox Code Playgroud)
我想将字符串的每个字符传递给printLetter函数.
我试过了:
int n = sizeof(word);
for (int i = 0; i < n-1; i++) {
printLetter(word[i]);
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:从'char'到'int*'的无效转换
我该怎么办?
谢谢!!
在参数类型错误的背后有一个更深层次的问题:缺少char和相应的映射int[]
.
重新定义printLetter
为
void printLetter(char letter)
Run Code Online (Sandbox Code Playgroud)
满足编译器,但本身并不能解决您的问题.无论是内部还是外部printLetter
,您都需要获得相应int[]
的内容char
.
实现这一目标的简单蛮力方法是使用a switch
,但更好的方法是使用第二个数组,即如下所示:
void printLetter(char letter) {
static int* charToMatrix[] = { A, B, C, ... };
int* matrixToPrint = charToMatrix[letter - 'A'];
// print the matrix
}
Run Code Online (Sandbox Code Playgroud)
请注意,这是一个示例 - 我现在无法访问C编译器,因此我不能保证它可以立即使用,但希望它能很好地说明这一点.它也缺少边界检查,因此它会在奇怪的随机位置访问内存,如果您尝试打印"未知"字符,可能会导致崩溃.
这个解决方案适用于大写字母; 如果你还需要打印小写字母或其他字符,你可能更喜欢使用256个元素的数组,其中只填充对应于"已知"矩阵的索引处的元素,其余的设置为NULL.