开发者

invalid conversion from 'char' to 'int* in C

I have the following arrays:

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 

And a function that prints the letters on a 5x3 LED matrix:

void printLetter(int letter[])

I have a string of letters:

char word[] = "STACKOVERFLOW";

and I want to pass each character of the string to the printLetter function.

I tried:

int n = sizeof(word);

for (int i = 0; i < n-1; i++) {  
    printLetter(word[i]);
}

But I get the following error: 开发者_JAVA技巧 invalid conversion from 'char' to 'int*'

What should i be doing?

Thanks!!


Behind the parameter type error there is a deeper issue: you lack the mapping between a char and the corresponding int[].

Redefining printLetter as

void printLetter(char letter)

satisfies the compiler, but doesn't solve your problem per se. Whether inside or outside printLetter, you need to get the corresponding int[] for a given char.

A simple brute-force way to achieve this would be to use a switch, but a better way is to use a second array, i.e. something like this:

void printLetter(char letter) {
  static int* charToMatrix[] = { A, B, C, ... };
  int* matrixToPrint = charToMatrix[letter - 'A'];
  // print the matrix
}

Note that this is an example - I don't have access to a C compiler right now, so I can't guarantee it works straight away, but hopefully it illustrates the point well enough. It also lacks bounds checking, so it accesses memory in strange random places, possibly resulting in a crash, if you attempt to print an "unknown" character.

This solution is supposed to work for the uppercase letters; if you need to print lowercase letters or other characters as well, you might prefer going with an array of 256 elements, where only the elements at indexes corresponding to the "known" matrices are filled, and the rest is set to NULL.


You cannot so easly transform the 'a' from "stackoverflow" to the A - the array of ints. You can define all the arrays that represent a letter in one single letter and get them by the convertion of your letter to int.


What you need to do is convert from a character to one of your arrays. So when you have the letter 'A', you'll want to use array A. The easiest way to do this is via lookup table.

int *lookup[256];  // assuming ASCII
memset(lookup, 0, sizeof(lookup));

lookup['A'] = A;
lookup['B'] = B;
...

Then when you have a character, you can get the proper array:

void printletter(char c);
{
    int *data = lookup((unsigned char)c);

    // In case you get a letter that you don't know how to display
    if (data != NULL)
    {
        // display with data
    }
}

Instead of building up your array at runtime, you can also build up your array at compile time although it will be a bit harder as you will need to manually put in NULL pointers.

int *lookup[256] = {
    NULL,   // you need a total of 65 NULL's
    NULL,
    ...
    A,      // so this is at the correct position
    B,
    C,
    ...
};


The function is declared as void printLetter(int letter[]), which means it takes a pointer to an array of ints. On the other hand, word is an array of chars, and word[i] is a char, which is not at all the right type. If printLetter() is really just supposed to print a single character, you should change its argument to be a char.


void printLetter(int letter[]) 

should be void printLetter(char letter)

Because: word is a char[] word[i] is a character.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜